2016-08-20 35 views
0

給定值I由POS項目,但是當我保存數據來訪問數據庫中示出了錯誤:vb.net消息:沒有用於一個或多個必需參數

沒有爲一個或多個所需的給定值參數。

我的代碼是:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 

    Dim MyConnection As OleDb.OleDbConnection = Nothing 
    Dim MyTransaction As OleDb.OleDbTransaction = Nothing 

    Try 
     'create the connection and transaction object 
     MyConnection = New OleDb.OleDbConnection(My.Settings.dbConnectionString) 
     MyConnection.Open() 
     MyTransaction = MyConnection.BeginTransaction 

     'insert the new recipt 
     Dim SQL As String = "insert into recipts (ReciptDate, ReciptTotal) values (:0,:1)" 
     Dim CMD1 As New OleDb.OleDbCommand 
     CMD1.Connection = MyConnection 
     CMD1.Transaction = MyTransaction 
     CMD1.CommandText = SQL 
     CMD1.Parameters.AddWithValue(":0", Now.Date) 
     CMD1.Parameters.AddWithValue(":1", TextBox4.Text) 
     CMD1.ExecuteNonQuery() 
     CMD1.Dispose() 

     'get the id for the recipt 
     SQL = "Select max (reciptId) as MAXID from recipts" 
     Dim CMD2 As New OleDb.OleDbCommand 
     CMD2.Connection = MyConnection 
     CMD2.Transaction = MyTransaction 
     CMD2.CommandText = SQL 
     Dim ReciptID As Long = CMD2.ExecuteScalar() 
     CMD2.Dispose() 


     'insert the details of the recipt 
     Dim I As Integer 
     For I = 0 To DGV2.Rows.Count - 1 

      'get the values 
      Dim Barcode As String = DGV2.Rows(I).Cells(0).Value 
      Dim BuyPrice As Decimal = DGV2.Rows(I).Cells(2).Value 
      Dim SellPrice As Decimal = DGV2.Rows(I).Cells(3).Value 
      Dim ItemCount As Integer = DGV2.Rows(I).Cells(4).Value 

      'next create a command 
      Dim CMD3 As New OleDb.OleDbCommand 
      SQL = "insert into ReciptDetails" & _ 
       "(ReciptId, Barcode,ItemCount,ItemBuyPrice,ItemSellPrice)" & _ 
       "Values" & _ 
       "(:0    ,:1    ,:2   ,:3    ,:4)" 
      CMD3.Connection = MyConnection 
      CMD3.Transaction = Mytransaction 
      CMD3.CommandText = SQL 
      CMD1.Parameters.AddWithValue(":0", ReciptID) 
      CMD1.Parameters.AddWithValue(":1", Barcode) 
      CMD1.Parameters.AddWithValue(":2", ItemCount) 
      CMD1.Parameters.AddWithValue(":3", BuyPrice) 
      CMD1.Parameters.AddWithValue(":4", SellPrice) 

      CMD3.ExecuteNonQuery() 
      CMD3.Dispose() 

     Next 

     'all well save the changes 
     Mytransaction.Commit() 

     'close conncetion 
     Mytransaction.Dispose() 
     MyConnection.Close() 
     MyConnection.Dispose() 

     DGV2.Rows.Clear() 
     TextBox4.Text = "" 

    Catch ex As Exception 
     If Mytransaction IsNot Nothing Then 
      Mytransaction.Rollback() 
     End If 
     If MyConnection IsNot Nothing Then 
      If MyConnection.State = ConnectionState.Open Then 
       MyConnection.Close() 
      End If 
     End If 

      MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly) 
    End Try 
End Sub 

什麼這裏的問題是什麼?

+0

在哪一行,你得到的錯誤 – FloatingKiwi

回答

0

簡單的錯字。在你CMD3您要添加的所有參數CMD1

因此CMD3已經5缺少的參數。

+0

謝謝你,親愛的,我自豪地爲您解決問題。 –

相關問題