Jump to content

New to ASP


raghup

Recommended Posts

I have edited the http://www.w3schools.com/ado/demo_db_edit.asp to suit my database.One of the fields is QTY. I have added two text boxes addTo and subtractFrom to this. How do I make the code work for adding or subtracting.the following is the code<%no=Request.Form("no")if no="" then response.endset conn=Server.CreateObject("ADODB.Connection") conn.provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("/aspTEST/dBase/stock.mdb"))set rs = Server.CreateObject("ADODB.Recordset")rs.Open "Select * from tblPUBLICATIONS where tblPUBLICATIONS.[no]=" & no , conn%><html><head><title>Edit or Delete Record In PUBLICATIONS</title><style type="text/css"><!--.style1 {color: #FF0000}--></style></head><body><center><h2>Edit or Delete Record number <h1><%=no%></h1> In PUBLICATIONS</h2><form method="post" action="PUBLICATIONSsubmit.asp" target="_self"><input name="no" type="hidden" value=<%=no%>><table bgcolor="#b0c4de"><%for each x in rs.Fields if x.name <> "no" and x.name <> "dateStamp" and x.name <> "ynDelete" then%> <tr> <td><%=ucase(x.name)%> </td> <td><input name="<%=x.name%>" value="<%=x.value%>" size="50"></td> <%end ifnextrs.closeconn.close%></tr><tr><td>ADD</td><td><input name ="addTo" value="" size="5"></td></tr><tr><td>SUBTRACT</td><td><input name ="subtractFrom" value="" size="5"></td></tr></table><br /><input type="submit" name="action" value="Save"><input type="submit" name="action" value="Delete"><input type=button value="Close Window" onClick="java script:self.close();"></form><h2 class="style1">Warning: You can not undo DELETE!!</h2></center></body></html>I edited the http://www.w3schools.com/ado/showcode.asp?...o_db_submit.asp page as follows<html><head><title>Edit or Delete Record From PUBLICATIONS</title><style type="text/css"><!--.style1 {color: #FF0000}--></style></head><body><center><h2>Edit or Delete Record From PUBLICATIONS</h2><%on error resume nextset conn=Server.CreateObject("ADODB.Connection") conn.provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("/aspTEST/dBase/stock.mdb"))if Request.form("action")="Save" then no=Request.Form("no") sql="UPDATE tblPUBLICATIONS SET PUBLICATION='" & Request.Form("PUBLICATION") & "'," sql=sql & "EffectiveDate='" & Request.Form("EffectiveDate") & "'," sql=sql & "COMMENTS='" & Request.Form("COMMENTS") & "'," QTY = Request.Form("QTY") addTo = Request.Form("addTo") subtractFrom = Request.Form("subtractFrom") if(addTo > 0 && subtractFrom > 0) then { Response.Write("You can not input in both the text boxes at the same time") } else { if (addTo > 0) then { QTY = QTY + addTo } else (subtractFrom > 0) { QTY = QTY - subtractFrom } } sql=sql & "QTY='" & "QTY" & "'," sql=sql & "dateStamp='" & now() & "' WHERE tblPUBLICATIONS.[no]=" & no conn.Execute sql, Recordsaffected if err <> 0 then Response.Write("You do not have permission to update this database!") else Response.Write("Record number " & no & " was updated.") end if ' Response.Write("Submitting records has been disabled from this demo")end ifif Request.Form("action")="Delete" then no=Request.Form("no") sql="UPDATE tblPUBLICATIONS SET ynDelete='" & 1 & "'," sql=sql & "dateStamp='" & now() & "' WHERE tblPUBLICATIONS.[no]=" & noconn.Execute sql, Recordsaffected'conn.Execute "DELETE FROM tblPUBLICATIONS WHERE tblPUBLICATIONS.[no]=" & no, Recordsaffected if err <> 0 then Response.Write("You do not have permission to delete a record from this database!") else Response.Write("Record number " & no & " was deleted.") end if ' Response.Write("Deleting records has been disabled from this demo")end ifconn.close%><form><input type=button value="Close Window" onClick="java script:self.close();"></form><h2 class="style1">Warning: You can not undo DELETE!!</h2></center></body></html>It is not working. Any help would be appreciated.Thank youcheersraghu

Link to comment
Share on other sites

That's a good call. This:

if(addTo > 0 && subtractFrom > 0) then{Response.Write("You can not input in both the text boxes at the same time")} else{if (addTo > 0) then {QTY = QTY + addTo} else (subtractFrom > 0){QTY = QTY - subtractFrom}}

Is not VBScript, the rest of the code is.

Link to comment
Share on other sites

The page cannot be displayed There is a problem with the page you are trying to reach and it cannot be displayed. --------------------------------------------------------------------------------Please try the following:Click Search to look for information on the Internet. You can also see a list of related sites. HTTP 500 - Internal server error Internet Explorer

Link to comment
Share on other sites

That message indicates a server error, in this case the error is a VBScript syntax error because the code is not VBScript. If you use a browser other then IE you should be able to see the specific error message.

Link to comment
Share on other sites

Tried this and is giving the same error message.if(addTo > 0 && subtractFrom > 0) then Response.Write("You can not input in both the text boxes at the same time") else if (addTo > 0) then QTY = QTY + addTo else if (subtractFrom > 0) QTY = QTY - subtractFrom end ifCheersRaghu

Link to comment
Share on other sites

Yeah, you're almost there. If you check the page using Firefox or Opera you should see a specific error message and line number where the error is, IE will not show those by default. There is an option in IE to disable that behavior.This code, which is a mashup of vb script:

if(addTo > 0 && subtractFrom > 0) then{Response.Write("You can not input in both the text boxes at the same time")} else{if (addTo > 0) then {QTY = QTY + addTo} else (subtractFrom > 0){QTY = QTY - subtractFrom}}

can be translated to VBScript as this:

if addTo > 0 and subtractFrom > 0 then  Response.Write("You can not input in both the text boxes at the same time")else  if addTo > 0 then 	QTY = QTY + addTo  else 	if subtractFrom > 0 then	  QTY = QTY - subtractFrom	end if  end ifend if

Link to comment
Share on other sites

thanks works partly now.when i try to add it is adding as follows80 + 2 = 802 and not 82and when i try to subtract it doing as follows80-2 = 802 and not 78what could be wrong?cheersRaghu
Its just a casting issue. When you get your variables from the querystring, you're not casting them to a particular datatype, so they are treated as strings. Change this code:
QTY = Request.Form("QTY")addTo = Request.Form("addTo")subtractFrom = Request.Form("subtractFrom")

To this:

QTY = cint(Request.Form("QTY"))addTo = cint(Request.Form("addTo"))subtractFrom = cint(Request.Form("subtractFrom"))

The cint function is a conversion function that casts a string as an integer.

Link to comment
Share on other sites

Does not update. I get this message "You do not have permission to update this database!"In the database I have set Required to "NO" and Allow zero length to "YES"So if this field is blank it shouldn't matter. But the record is not updating.cheersRaghu

Link to comment
Share on other sites

Does not update. I get this message "You do not have permission to update this database!"In the database I have set Required to "NO" and Allow zero length to "YES"So if this field is blank it shouldn't matter. But the record is not updating.cheersRaghu
Its not a problem with the record, its a problem with permissions: in other words, you don't have write-permissions on your database or on the folder where your database resides.I'm pretty sure you're using an access database, then you need to CHMOD the directory where your database is located to rw-------, and CHMOD the .mdb file to rw-------.If you don't know how to do that, then you have a few options:- If your files are on your local server, open up IIS and navigate to the folder and file where your database is located. Right click and choose "Properties", and change the permissions as needed.- If your files are on a remote server, then use an FTP folder to navigate to the folder and file where your database is located. Your FTP will usually have a button or menu option called CHMOD that will allow you to change the permissions of a file or directory. If you can't CHMOD your files from your FTP, email your host and ask them to allow write permissions on the required folders and files.
Link to comment
Share on other sites

You would think that, but it's not even a permissions problem. It's this code:

conn.Execute sql, Recordsaffectedif err <> 0 thenResponse.Write("You do not have permission to update this database!")else Response.Write("Record number " & no & " was updated.")end if ' Response.Write("Submitting records has been disabled from this demo")end if

It is writing out the permission error message because an error happened with the query (or, actually, because err does not equal 0). The code says that any time there's an error to write out the permission denied message, which isn't very good logic. Instead of writing out that message, write out the error text from the err object. If you do that you should see why the query is failing. I'm not even positive that will work, if err is an object (which I believe it is), it will never be equal to 0, even if no error occured. In that case, the logic is just flawed in the if statement.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...