先生, 我試圖插入DATAS數據庫 我使用VB2008和SQL作爲後端錯誤插入數據網格視圖數據
錯誤是「變量名‘@item’已聲明。變量名稱在查詢批處理或存儲過程中必須是唯一的。「
這裏是我的代碼
Private Sub ButtonSubmit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSubmit.Click
con = New SqlConnection(s)
con.Open()
sql = "INSERT INTO TableSpecial (RequestNo, RegNo, JobNumber, Kilometer, Preparedby, GzlNo, Chassis, Engine, VehicleType, Division, Createdby, Date) VALUES (@RequestNo, @RegNo, @JobNumber, @Kilometer, @Preparedby, @GzlNo, @Chassis, @Engine, @VehicleType, @Division, @Createdby, @Date)"
cmd = New SqlCommand(sql, con)
cmd.Parameters.Add("@RequestNo", SqlDbType.NChar).Value = Trim(LabelReqNo.Text)
cmd.Parameters.Add("@RegNo", SqlDbType.NChar).Value = Trim(ListBoxRegNo.SelectedItem)
cmd.Parameters.Add("@JobNumber", SqlDbType.NChar).Value = Trim(TextBoxJobNo.Text)
cmd.Parameters.Add("@Kilometer", SqlDbType.NChar).Value = Trim(TextBoxKM.Text)
cmd.Parameters.Add("@Preparedby", SqlDbType.NChar).Value = Trim(TextBoxReqby.Text)
cmd.Parameters.Add("@Createdby", SqlDbType.NChar).Value = Trim(TextBoxPreparedby.Text)
cmd.Parameters.Add("@GzlNo", SqlDbType.NChar).Value = Trim(LabelGzlNo.Text)
cmd.Parameters.Add("@Division", SqlDbType.NChar).Value = Trim(LabelDivision.Text)
cmd.Parameters.Add("@Chassis", SqlDbType.NVarChar).Value = Trim(LabelChassis.Text)
cmd.Parameters.Add("@Engine", SqlDbType.NVarChar).Value = Trim(LabelEngine.Text)
cmd.Parameters.Add("@VehicleType", SqlDbType.NVarChar).Value = Trim(LabelVehicleType.Text)
cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = LabelTime.Text
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
For i As Integer = 0 To DataGridView1.RowCount - 1
cmd.Parameters.Add("Item", SqlDbType.NVarChar).Value = Me.DataGridView1.Rows(i).Cells("Itemm").Value
cmd.Parameters.Add("Qty", SqlDbType.NChar).Value = Me.DataGridView1.Rows(i).Cells("Qtyy").Value
Next
'cmd.Parameters.Add("@Item", SqlDbType.NVarChar).Value = row.Cells(0).Value.ToString
' cmd.Parameters.Add("@Qty", SqlDbType.NChar).Value = row.Cells(1).Value.ToString
End If
Next
Dim result As Integer
result = cmd.ExecuteNonQuery()
If result = 1 Then
MsgBox("Saved!!", MsgBoxStyle.Information, _
"Success")
Else
MsgBox("Updation incomplete!!", MsgBoxStyle.Information, _
"Updation not complete")
End If
con.Close()
Autogenerate_ReqNo()
End Sub