user2780962
user2780962

Reputation: 113

update statement not working although code is right

my update statement not working .. there is no effect on database when i check it

here is the code

conn.Open()

    Try
        Dim update As New OleDbCommand
    update.Connection = conn
        update.CommandText = " update O_name set fname = ' " & Name1.Text & " ' where ID = ' " & ID.Text & " ' "
        update.ExecuteNonQuery()
        MsgBox("done")
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try
    conn.Close()

Upvotes: 0

Views: 246

Answers (1)

Damith
Damith

Reputation: 63065

Using connection As New OleDbConnection(connectionString)
Using command As New OleDbCommand("update O_name set fname =? where ID =?", connection)
   command.Parameters.AddWithValue("p1", Name1.Text)
   command.Parameters.AddWithValue("p2",  ID.Text)
   command.Connection.Open()
   command.ExecuteNonQuery()
   MsgBox("done")
End Using 
End Using 

use parameters but you need to specify parameters by using ?, because :

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.

UPDATE:

You have additional spaces in your parameter value assign, try below

update.CommandText = String.Format("UPDATE O_name SET fname ='{0}' WHERE ID ='{1}'",Name1.Text, ID.Text)

Upvotes: 2

Related Questions