必须声明标量变量“@ZeroQty”

时间:2015-08-04 18:01:51

标签: sql vb.net vb.net-2010

保存记录时出现标量问题会产生错误 这是我的代码

    Try
        ' Dim updatedb As String = " INSERT tblStock SET StockCode = @StockCode,SupplierRef = @SupplierRef,Season = @Season,DeadCode = @DeadCode,DeliveredQtyHangers = @DeliveredQtyHangers,RemoveFromClearance = @RemoveFromClearance,AmountTaken= @AmountTaken, CostValue = @CostValue,PCMarkUp = @PCMarkUp,ZeroQty= @ZeroQty,CreatedBy = @CreatedBy,CreatedDate= @CreatedDate WHERE StockCode = @StockCode"
        Dim insertdb As String = " INSERT INTO tblStock (StockCode,SupplierRef,Season,DeadCode,RemoveFromClearance,AmountTaken,DeliveredQtyHangers,CostValue,PCMarkUp,ZeroQty,CreatedBy,CreatedDate)VALUES(@StockCode,@SupplierRef, @Season, @DeadCode,@RemoveFromClearance,@AmountTaken,@DeliveredQtyHangers,@CostValue,@PCMarkUp,@ZeroQty,@CreatedBy,@CreatedDate)"
        Dim connectionString As String = "Data Source=.\SQLExpress;Initial Catalog=StockMasterv2;Trusted_Connection=True"
        Dim connection As New SqlConnection(connectionString)
        ' Create a DataSet
        Me.Validate()
        Dim com As New SqlCommand(insertdb, connection)
        com.Connection.Open()
        com.Parameters.AddWithValue("@StockCode", TextBox1.Text)
        com.Parameters.AddWithValue("@SupplierRef", TextBox2.Text)
        com.Parameters.AddWithValue("@Season", cboSeasons.Text)
        com.Parameters.AddWithValue("@DeadCode", CheckBox1.CheckState)
        com.Parameters.AddWithValue("@RemoveFromClearance", CheckBox2.CheckState)
        com.Parameters.AddWithValue("@AmountTaken", TextBox3.Text)
        com.Parameters.AddWithValue("@ZeroQty,", CheckBox3.CheckState)
        com.Parameters.AddWithValue("@CostValue", TextBox4.Text)
        com.Parameters.AddWithValue("@PCMarkUp", TextBox5.Text)
        com.Parameters.AddWithValue("@CreatedBy", TextBox6.Text)
        com.Parameters.AddWithValue("@CreatedDate", DateTimePicker1.Value)
        com.Parameters.AddWithValue("@DeliveredQtyHangers", TextBox7.Text)
        com.ExecuteNonQuery()
        com.Connection.Close()
        Form1.DataViewer.Refresh()
        MsgBox("Record Created Successfully", MsgBoxStyle.Information, "Stock Master v2")
    Catch ex As SqlException
        MsgBox("Update Failed because of" & vbCrLf & ex.ErrorCode & "  " & ex.Message, MsgBoxStyle.Information, "Stock Master v2")
    End Try
End Sub

我知道很久也在使用insert命令时如果只有文本框和组合框则不保存到数据库

2 个答案:

答案 0 :(得分:3)

AddWithValue

中变量末尾有一个额外的逗号
com.Parameters.AddWithValue("@ZeroQty,", CheckBox3.CheckState)
                                     ^---Here

删除它,它应该可以工作。

答案 1 :(得分:0)

你的意思是你的第一个SQL语句中的UPDATE吗?

" UPDATE tblStock SET StockCode....

也许您应该尝试将变量@ZeroQty设置为

CheckBox3.CheckState.ToString()