4 Replies - 2128 Views - Last Post: 12 December 2012 - 06:14 PM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Whats wrong with this parameterized update query?

Posted 12 December 2012 - 02:08 PM

I am trying to use a parameterized query to update entries in a mysql database, however it wont update anything.

This is the code i am using:

sqlString = "UPDATE table SET field1 = @value1, field2 = @value2, field3 = @value3 WHERE field1 = @currentValue"
            
            dbCommand.CommandText = sqlString
            dbCommand.Parameters.AddWithValue("@value1", txtOne.Text)
            dbCommand.Parameters.AddWithValue("@value2 ", txtTwo.Text)
            dbCommand.Parameters.AddWithValue("@value3 ", txtThree.Text)
            dbCommand.Parameters.AddWithValue("@currentValue", lstListBox.SelectedValue)
            dbCommand.ExecuteNonQuery()


Yes i connect to the database, yes i open the connection. I have a SELECT statement that displays the data from the database so i know everything is set up to connect to it fine. The problem seems to lie with the parameters, when i tried to put single quotes ' ' around the @value1 parameter it ignore the parameter and added @value1 to the database so the update statement seems to work.

Can anyone tell me what is wrong with it?

This post has been edited by robgw3: 12 December 2012 - 05:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Whats wrong with this parameterized update query?

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Whats wrong with this parameterized update query?

Posted 12 December 2012 - 04:45 PM

You're update statement is looking for @value but you have @field in two of your parameters
Was This Post Helpful? 0
  • +
  • -

#3 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Whats wrong with this parameterized update query?

Posted 12 December 2012 - 05:02 PM

View PostNakor, on 12 December 2012 - 11:45 PM, said:

You're update statement is looking for @value but you have @field in two of your parameters


Oh that, no those are actually right, i just changed out the actual parameters and values for field# and @value# and guess i made a mistake when i was changing those two. In my code they actually match, but even so that would either cause an error or update only the one correct one. In this case nothing at all is happening.
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Whats wrong with this parameterized update query?

Posted 12 December 2012 - 06:08 PM

i'm not seeing anything that looks out of place in that little bit of code you've provided. If you debug it and step through the code execution does everything seem to work correctly?
Was This Post Helpful? 0
  • +
  • -

#5 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Whats wrong with this parameterized update query?

Posted 12 December 2012 - 06:14 PM

Well i put single quotes around the @values in the UPDATE statement and it updated the database with @value1, @value2 and @value3 so the update statement does work, it just doesnt seem to do it with the paramterized query.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1