Home > Net >  Why wont my SET value WHERE SQL Statement not work
Why wont my SET value WHERE SQL Statement not work

Time:01-27

Currently cant get this to work, despite it being almost for verbatim the same as else where in my code.

  Using con As New OleDbConnection(constring)

            Using cmd As New OleDbCommand("UPDATE " & "`" & "SIQPERSIST" & "`" & " SET [Date_Added] = @Date_Added WHERE [BatchName] = @BatchName", con)

                cmd.Parameters.AddWithValue("@BatchName", BatchName2)
                cmd.Parameters.AddWithValue("@Date_Added", Date.Now.ToShortDateString)

                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()


            End Using
        End Using

I'm working in Vb.net

and i need to update all rows that have the name BatchName2 (this comes from a textbox) with the current date. The table they are on is SIQPERSIST.

The error i get is that its missing a parameter.

But i have don't know what parameter it needs despite almost similar code working else where, except the working code uses a WHERE KEY= 'keynumber' statement.

CodePudding user response:

The issue is this uses backticks for the concatenated variable. Remember, ` and ' are not the same thing, and only one of those would work here.

It should look like something more like this:

Using con As New OleDbConnection(constring)
Using cmd As New OleDbCommand("UPDATE SIQPERSIST SET [Date_Added] = Date() WHERE [BatchName] =  @BatchName", con)
    cmd.Parameters.AddWithValue("@BatchName", BatchName2)
    con.Open()
    cmd.ExecuteNonQuery()
End Using
End Using

Note, there's no need to call con.Close() when you have a Using block to take care of that for you.

Additionally, not only did I convert BatchName2 to a real query parameter (Shame on you for adding concatenation to a query that already demonstrates how to use parameters!), but I was also able to convert the existing parameter to use get the date in the DB itself.

  • Related