wwwMarvscom wwwMarvscom - 2 months ago 7
Vb.net Question

No value given for one or more required parameters when using the `UPDATE` command

I'm trying to update my records using the

UPDATE
command. I get this error:


No value given for one or more required parameters.


Here's my code:

Private Sub saveOle_Click(sender As Object, e As EventArgs) Handles saveOle.Click
Try
Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\samplelangtowalangya.mdb;")
conn.Open()
Dim command As New OleDbCommand("UPDATE sample SET ewan = @ewan, ko = @ko, sayo = @sayo, hehehe = @hehehe WHERE Number_of_Employees = @Number_of_Employees", conn)
With command.Parameters
.AddWithValue("@ewan", ewan.Text)
.AddWithValue("@ko", ko.Text)
.AddWithValue("@sayo", sayo.Text)
.AddWithValue("@hehehe", hehehe.Text)
command.ExecuteNonQuery()
End With

MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
command.Dispose()
conn.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub


The
Number_of_Employees
is my ID and primary key.

Answer

So I tried to placed my ID after the four fields, and it works fine now :)

 Private Sub saveOle_Click(sender As Object, e As EventArgs) Handles saveOle.Click
    Try
        Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\samplelangtowalangya.mdb;")
            conn.Open()
            Dim command As New OleDbCommand("UPDATE sample SET ewan = @ewan, ko = @ko, sayo = @sayo, hehehe = @hehehe WHERE Number_of_Employees = @Number_of_Employees", conn)
            With command.Parameters
                .AddWithValue("@ewan", ewan.Text)
                .AddWithValue("@ko", ko.Text)
                .AddWithValue("@sayo", sayo.Text)
                .AddWithValue("@hehehe", hehehe.Text)
                .AddWithValue("@Number_of_Employees", IDtext.Text)
            End With
            command.ExecuteNonQuery()
            MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            command.Dispose()
            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub