0

I'm making a sales report (POS). I have combobox where the items are overall, daily, monthly, yearly. So if the user picks on the coverage (combobox), for example he pick the Monthly, the sales report of Monthly will be displayed on the datagridview. Now, my problem is how to solve this error: 'Error: The parametized query '(@Month nvarchar(4000)) SELECT * FROM tbltransactions INNER JOIN' expects the parameter '@Month', which was not supplied.'

TableStructure

Code for retrieving the monthly sales:

Public Sub retrieveMonthlySales()
        Try
            dbConnection()
            search_query = "SELECT * FROM tbltransactions INNER JOIN tbltransaction_logs ON tbltransaction_logs.trans_number = tbltransactions.trans_number WHERE Month = @Month;"
            command = New SqlCommand
        With command
            .Connection = connection
            .CommandText = search_query
            .Parameters.Clear()
            .Parameters.AddWithValue("@Month", salesView.comboMonth.SelectedItem)
            .ExecuteNonQuery()
        End With
        dataTable = New DataTable
        dataAdapter = New SqlDataAdapter
        dataAdapter.SelectCommand = command
        dataAdapter.Fill(dataTable)
        salesView.salesDGV.DataSource = dataTable

        With salesView.salesDGV
            .Columns(1).HeaderText = "Invoice #"
            .Columns(7).HeaderText = "Quantity"
            .Columns(8).HeaderText = "Product Size"
            .Columns(9).HeaderText = "Product Code"
            .Columns(12).HeaderText = "Total Amount"
        End With
    Catch ex As SqlException
        MsgBox("Error: " + ex.Message)
    Finally
        connection.Close()
        command.Dispose()
    End Try
End Sub

Code for combobox

Private Sub comboCoverage_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboCoverage.SelectedIndexChanged
    If comboCoverage.SelectedItem = "Monthly" Then
        retrieveMonthlySales()
        MsgBox("Showing monthly sales!")
        comboMonth.Enabled = True
        comboDay.Enabled = False
        comboYear.Enabled = False
        btnSearch.Enabled = True
        comboMonth.SelectedItem = ""
        comboDay.SelectedItem = ""
        comboYear.SelectedItem = ""
    End If
End Sub
Jhon Th
  • 15
  • 1
  • 6

1 Answers1

0

Make sure that salesView.comboMonth.SelectedItem value is not null in the line .Parameters.AddWithValue("@Month", salesView.comboMonth.SelectedItem)

You can initialize it with empty string or use DBNull.Value.

See this answer

sigeje
  • 328
  • 3
  • 8