Сообщение vb.net: для одного или нескольких обязательных параметров не задано значение

vb.net

29 просмотра

1 ответ

Я сделал проект POS, но когда я сохраняю данные для доступа к базе данных, отображается ошибка:

не указано значение для одного или нескольких обязательных параметров.

Мой код:

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

В чем здесь проблема?

Автор: ak_ter Источник Размещён: 08.11.2019 10:58

Ответы (1)


0 плюса

Простая опечатка. В вашем CMD3 вы добавляете все параметры в CMD1

Следовательно, CMD3 имеет 5 пропущенных параметров.

Автор: FloatingKiwi Размещён: 20.08.2016 07:19
Вопросы из категории :
32x32