Pass user input from form To Report Viewer using SQL SELECT ... @var


Dec 30, 2015
Programming Experience
Here is my final code.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WinForms 

Public Class REPORT_monthly_Procurement_goods
    Dim conn As SqlConnection
    Dim comm As SqlCommand
Private Sub openconnection()
        conn = New SqlConnection
        conn.ConnectionString = "server=PRIVATE;database=PRIVATE;Integrated Security=SSPI;"
    End Sub

    Private Sub REPORT_monthly_Procurement_goods_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Select the current year in the ComboBox
        Dim YearStr As String = DateTime.Now.Year.ToString()

        ComboBox_YEAR.SelectedIndex = 0I
        For Counter As Integer = 0I To ComboBox_YEAR.Items.Count - 1I
            If ComboBox_YEAR.Items(Counter).ToString = YearStr Then ComboBox_YEAR.SelectedIndex = Counter
        Next Counter

        'Select the current month in the ComboBox
        ComboBox_YEAR.SelectedIndex = DateTime.Now.Month - 1

        Call LoadReport(DateTime.Now.Month, DateTime.Now.Year)
    End Sub

    Private Sub ComboBox_MONTH_SelectedIndexChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub ComboBox_YEAR_SelectedIndexChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub go_button_Click(sender As Object, e As EventArgs) Handles go_button.Click
        Dim YearValue As Integer
        If Not Integer.TryParse(ComboBox_YEAR.SelectedItem.ToString, YearValue) Then YearValue = DateTime.Today.Year
        Call LoadReport(GetMonthInteger(), YearValue)
    End Sub

    Private Sub LoadReport(MonthValue As Integer, YearValue As Integer)
            Me.procurement_goodsTableAdapter.Fill(Me.DSOURCE_ProcurementGoodsTABLE.procurement_goods, MonthValue, YearValue)
        Catch Ex As Exception
            MessageBox.Show(String.Format("Error getting data for the report:{0}{1}", Environment.NewLine, Ex.ToString()))
        End Try
    End Sub

    Private Function GetMonthInteger() As Integer
        Dim MonthValue As Integer

        Select Case ComboBox_MONTH.SelectedItem.ToString
            Case "January" : MonthValue = 1I
            Case "February" : MonthValue = 2I
            Case "March" : MonthValue = 3I
            Case "April" : MonthValue = 4I
            Case "May" : MonthValue = 5I
            Case "June" : MonthValue = 6I
            Case "July" : MonthValue = 7I
            Case "August" : MonthValue = 8I
            Case "September" : MonthValue = 9I
            Case "October" : MonthValue = 10I
            Case "November" : MonthValue = 11I
            Case "December" : MonthValue = 12I
            Case Else : MonthValue = 0I
        End Select

        Return MonthValue
    End Function

    Private Sub ReportViewer1_Load(sender As Object, e As EventArgs) Handles ReportViewer1.Load

    End Sub

    Private Sub ComboBox_YEAR_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox_YEAR.SelectedIndexChanged

    End Sub

End Class
Top Bottom