Reputation: 823
I have a problem with updating a SQL Server table from a DataGridView in the VB.Net, I know that the exception that I had is because of Null value but I'm sure that the value is set with a valid value, here is the code:
cmd.CommandText = _
"update BillItem set Item=@item1,Quantity=@q1,Price=@price1,TotalPrice=@tp1,BillID=@billId1 where Id=@id1"
cmd.Parameters.Add("@item1", SqlDbType.NVarChar)
cmd.Parameters.Add("@q1", SqlDbType.Int)
cmd.Parameters.Add("@price1", SqlDbType.Float)
cmd.Parameters.Add("@tp1", SqlDbType.Float)
cmd.Parameters.Add("@billId1", SqlDbType.Int)
cmd.Parameters.Add("@Id1", SqlDbType.Int)
connection.Open()
cmd.Connection = connection
For i As Integer = 0 To 1
cmd.Parameters(0).Value = DataGridView1.Rows(i).Cells(1).Value
cmd.Parameters(1).Value = DataGridView1.Rows(i).Cells(2).Value
cmd.Parameters(2).Value = DataGridView1.Rows(i).Cells(3).Value
cmd.Parameters(3).Value = Integer.Parse(DataGridView1.Rows(i).Cells(2).Value) * Double.Parse(DataGridView1.Rows(i).Cells(3).Value)
cmd.Parameters(4).Value = Integer.Parse(CurrentBillIDLbl.Text)
cmd.Parameters(5).Value = DataGridView1.Rows(i).Cells(0).Value
cmd.ExecuteNonQuery()
Next
connection.Close()
Here is the exception:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: The parameterized query '(@item nvarchar(4),@quantity int,@price float,@tp float,@billId ' expects the parameter '@q1', which was not supplied.
Upvotes: 0
Views: 2730
Reputation: 11514
Your CommandText
parameters do not match the actual parameters as seen in the error message. The actual query thinks has @item
but your example has @item1
. Likewise, @q1
is not @quantity
.
I don't think the code you have shown is in context, you are not using the command text that you think you are.
Upvotes: 3