p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   UPDATE statement (http://p2p.wrox.com/showthread.php?t=55062)

takwirira March 7th, 2007 04:03 PM

UPDATE statement
 
hie guys, im having trouble with updating an access database. Here is my code below. userAns and QuesNo are fields in the database and userAns and ques are variables that I have declared and will get values from textboxes etc. For some reason the details are not being updated into the database

        Dim objConn As New OleDbConnection _
            ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "User ID=Admin;" & _
            "Data Source=db2.mdb")
        objConn.Open()

        Dim objAdapter As New OleDbDataAdapter
        objAdapter.UpdateCommand = New OleDbCommand( _
            "UPDATE Ques_Table SET userAns= '" & userAns & "' Where QuesNo ='" & ques & "'", objConn)

        objConn.Close()


dparsons March 7th, 2007 04:06 PM

Write out your SQL statement, are all the values as they should be?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========

takwirira March 7th, 2007 04:10 PM

Dim userAns As String
   userAns = yourAns.text
Dim ques As String
   ques = quesNumber.text

Dim objConn As New OleDbConnection _
            ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "User ID=Admin;" & _
            "Data Source=db2.mdb")
        objConn.Open()

        Dim objAdapter As New OleDbDataAdapter
        objAdapter.UpdateCommand = New OleDbCommand( _
            "UPDATE Ques_Table SET userAns= '" & userAns & "' Where QuesNo ='" & ques & "'", objConn)

        objConn.Close()


takwirira March 7th, 2007 04:13 PM

im not sure about my syntax and quotes e.g.'" & userAns & "'


dparsons March 7th, 2007 04:33 PM

thats not what i meant. Instead of executing the sql statement do:

Response.Write("UPDATE Ques_Table SET userAns= '" & userAns & "' Where QuesNo ='" & ques & "'")

Look at hte statement, does it produce the update statement you THINK its producing.

Also, by and large, when a table doesn't update the correct row, its normally because of a failure in your WHERE clause.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========

takwirira March 7th, 2007 04:42 PM

when I put in Response.Write("UPDATE Ques_Table SET userAns= '" & userAns & "' Where QuesNo ='" & ques & "'") it say End of Statement expected. If I got you right do I simply replace "UPDATE Ques_Table SET userAns= '" & userAns & "' Where QuesNo ='" & ques & "'", objConn) with Response.Write("UPDATE Ques_Table SET userAns= '" & userAns & "' Where QuesNo ='" & ques & "'") ???



dparsons March 7th, 2007 04:50 PM

The response.write is for purposes of debugging only. Your sql statement is not executing correctly, so my suggestion is to look at the SQL string itself.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========

click March 7th, 2007 07:48 PM

What he means is do a response.write of ONLY the sql syntax, and then read it, it should have your values etc in there, with not syntax errors.

I think you should really think close about what your doing though. Your leaving it open to a SQL injection attack. Thats where someone puts malicious code into a text box, and on submit, it runs against your database and can update/delete/destroy things your not aware.

That being said I ran your query and got this.
UPDATE Ques_Table SET userAns= 'hello' Where QuesNo ='world'
and the query will work as long as everything else is how it should be, which means either your connection isnt happenign properly, or your columns are not spelled correctly or the 'types' are not varchar, and the compare is failing based on type.




All times are GMT -4. The time now is 12:35 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.