Search code examples
c#visual-studio-2013parameter-passingsql-server-2014reportviewer

Set parameters in Report Viewer


I want my report to display all the products that have the stock less than the value that's inserted in the Search text box. There is error.

This is the stored procedure:

CREATE PROCEDURE [dbo].[GetInventoryReport]
    @Stock int
AS
    SELECT * 
    FROM Product
    WHERE Stock < @Stock
    ORDER BY Stock ASC

This is the code when user enter value in Search text box.

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    DataTable dtbl = GetInventoryReport_Result();

    rptInventoryStock.Visible = true;
    rptInventoryStock.LocalReport.ReportPath = "InventoryReport.rdlc";
    rptInventoryStock.LocalReport.DataSources.Clear();

    rptInventoryStock.LocalReport.Add(new ReportDataSource("IOOP_DatabaseDataSet", dtbl));

    this.rptInventoryStock.RefreshReport();
}

This is the function for GetInventoryReport_Result():

private DataTable GetInventoryReport_Result()
{
    DataTable dtbl = new DataTable();

    try
    {
        SqlCommand sqlCmd = new SqlCommand("GetInventoryReport", sqlCon);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@Stock", txtSearch.Text.Trim());

        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dtbl);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error Message");
    }
    finally
    {
        if (sqlCon != null)
            sqlCon.Close();
    }

    return dtbl;
}

Error:

'Microsoft.Reporting.WinForms.LocalReport' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'Microsoft.Reporting.WinForms.LocalReport' could be found (are you missing a using directive or an assembly reference?)


Solution

  • You missed .DataSources

    Replace:

    rptInventoryStock.LocalReport.Add(new ReportDataSource("IOOP_DatabaseDataSet", dtbl));
    

    With

    rptInventoryStock.LocalReport.DataSources.Add(new ReportDataSource("IOOP_DatabaseDataSet", dtbl));