Passing parameter to Telerik Report

Noelle picture Noelle · Nov 20, 2012 · Viewed 10k times · Source

I have a telerik report project added to my website and I'm trying to pass a parameter to it but I receive the following error

Error:

Object reference not set to an instance of an object.

Asp.net Code

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
    {
        MembershipCard rpt = new MembershipCard();
        // Error occuring here
        rpt.ReportParameters["@MemberID"].Value = "5"; // Request["MemberID"];
        ReportViewer1.Report = rpt;
    }        
}

Telerik Report Class

    public MembershipCard()
    {
        /// <summary>
        /// Required for telerik Reporting designer support
        /// </summary>
        InitializeComponent();
        this.DataSource = null;
    }

    private void MembershipCard_NeedDataSource(object sender, EventArgs e)
    {
        string proc = "rpt_MembershipCard" + this.ReportParameters["@MemberID"].Value.ToString();
        SqlDataAdapter adapter = new SqlDataAdapter(proc, "Data Source=(local); Initial Catalog= northwind; intergrated security = True");
        DataSet dataSet = new DataSet();
        adapter.Fill(dataSet);
        (sender as Telerik.Reporting.Processing.Report).DataSource = dataSet;
    }

Can someone please point me in the right direction I haven't been able to find anything relevant on the internet.

Answer

Dusan Vasiljevic picture Dusan Vasiljevic · Nov 20, 2012

Problem is that you are not providing parameters. Try changing:

rpt.ReportParameters.Add("MemberID", "5");

And:

string proc = this.ReportParameters["MemberID"].Value.ToString();

Also, if you added DataSet.xsd file in the solution, use its generated TableAdapter.

Example for stored procedure:

using System.Data;
using YourNamespace.StoredProcedureNameTableAdapter;

//Keep in mind StoredProcedureName is taken from SP name in your database
//when you add DataSet.xsd to your solution by default if you don't change anything

private void ClassName_NeedDataSource(object sender, EventArgs e)
{
   int someID = int.Parse(this.ReportParameters["ParameterID"].Value.ToString());
   StoredProcedureNameTableAdapter adapter = new StoredProcedureNameTableAdapter();
   DataSetFile.StoredProcedureNameDataTable data = adapter.GetData(someID); //DataSetFile is DataSetFile.xsd and GetData is method name you defined on DataSetFile.xsd creation
   ((Telerik.Reporting.Processing.Report)sender).DataSource = data.AsDataView();
}