Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 7th, 2007, 04:03 PM
Authorized User
 
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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()

 
Old March 7th, 2007, 04:06 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
 
Old March 7th, 2007, 04:10 PM
Authorized User
 
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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()

 
Old March 7th, 2007, 04:13 PM
Authorized User
 
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old March 7th, 2007, 04:33 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
 
Old March 7th, 2007, 04:42 PM
Authorized User
 
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 & "'") ???


 
Old March 7th, 2007, 04:50 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
 
Old March 7th, 2007, 07:48 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.







Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
Error when trying to use an UPDATE statement wayne62682 Access VBA 2 April 4th, 2006 07:50 PM
Update Statement some error thas123 ADO.NET 1 January 8th, 2006 10:30 AM
SQLDataAdapter's Update Statement bmains ADO.NET 5 May 7th, 2004 03:05 AM





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