1

我有一个 Telerik 报告项目添加到我的网站,我正在尝试将参数传递给它,但我收到以下错误

错误:

你调用的对象是空的。

ASP.NET 代码

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 报告类

    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;
    }

有人可以指出我正确的方向吗?我无法在互联网上找到任何相关的东西。

4

1 回答 1

1

问题是您没有提供参数。尝试改变:

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

和:

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

此外,如果您DataSet.xsd在解决方案中添加了文件,请使用其生成的TableAdapter.

存储过程示例:

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();
}
于 2012-11-20T13:40:52.473 回答