phil_19
phil_19

Reputation: 105

Vb.net Ms Access - No value given for one or more required parameters

What is the problem with my below code. It always showing an error: No value given for one or more required parameters. Thanks for help.

        Dbcon.Open()

        Dim query As String
        Dim dbup As New OleDbCommand

        For x As Integer = 0 To IncomingMailDGV.Rows.Count - 1
            Dim received As String = IncomingMailDGV.Rows(x).Cells(0).Value
            Dim subject As String = IncomingMailDGV.Rows(x).Cells(1).Value
            Dim contents As String = IncomingMailDGV.Rows(x).Cells(2).Value

            query = "SELECT ReceivedDateTime, Subject, MessageContents FROM IncomingAlerts WHERE ReceivedDateTime = @ReceivedDateTime AND MessageContents =@MessageContents"
            dbup = New OleDbCommand(query, Dbcon)
            dbup.Parameters.AddWithValue("ReceivedDateTime", received)
            dbup.Parameters.AddWithValue("MessageContents", contents)
            Dim readinputs As OleDbDataReader
            readinputs = dbup.ExecuteReader()

            If readinputs.HasRows = False Then

                Dim InsertData As String
                InsertData = "INSERT INTO IncomingAlerts(ReceivedDateTime, Subject, MessageContents) Values (@ReceivedDateTime, @Subject, @MessageContents)"
                dbup.Parameters.AddWithValue("ReceivedDateTime", received)
                dbup.Parameters.AddWithValue("Subject", subject)
                dbup.Parameters.AddWithValue("MessageContents", contents)
                dbup = New OleDbCommand(InsertData)

                With dbup
                    .CommandText = InsertData
                    .Connection = Dbcon
                    .ExecuteNonQuery()
                End With
            End If
        Next

Update:

Im using vb.net - datagridview and msaccess.

Upvotes: 0

Views: 197

Answers (1)

Saragis
Saragis

Reputation: 1792

Your issue is in this piece of code:

            dbup.Parameters.AddWithValue("ReceivedDateTime", received)
            dbup.Parameters.AddWithValue("Subject", subject)
            dbup.Parameters.AddWithValue("MessageContents", contents)
            dbup = New OleDbCommand(InsertData)

You are adding all the required parameters, and then initializing a new command object, which effectively clears all added the previously parameters. Adding the parameters after creating the command should solve this.

Upvotes: 1

Related Questions