我从VB.Net中的DataGridView更新SQL Server表时遇到问题,我知道我遇到的异常是因为Null值,但我确定该值设置为有效值,这是代码:
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()
以下是例外:
System.Data.dll中发生未处理的“System.Data.SqlClient.SqlException”类型异常
附加信息:参数化查询'(@item nvarchar(4),@ quantity int,@ price float,@ tp float,@ billId'需要参数'@ q1',这是未提供的。
答案 0 :(得分:3)
您的CommandText
参数与错误消息中显示的实际参数不匹配。实际查询认为有@item
,但您的示例有@item1
。同样,@q1
不是@quantity
。
我不认为您显示的代码是在上下文中,您没有使用您认为的命令文本。