2016-08-20 10 views
0

のために与えられていない値は、私は、POSプロジェクトを作ったが、私はデータベースにアクセスするためにデータを保存するときにエラーが表示されます。vb.netメッセージ:一つ以上の必要なパラメータ

1以上の必要のために与えられた値なしパラメーター。

私のコードは次のとおりです。

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

この問題を解決してくれたことを誇りに思います。 –

関連する問題