Search code examples
c#reportrdlcdynamic-rdlc-generation

Dynamically binding of Dataset to RDLC Reports


I would like to bind the dynamically dataset to the rdlc. I can view the report if I use inline DataSource in ASPX file (static binding). However, if I use the following codes, the Report viewer keeps showing "Loading.." Image.

I have already check the dataset name and if I changed the dataset name to "Orders2", it shows me that required dataset "Orders" is not provided. So, I add the GridView on the form and test my DataSet. The dataset contains data and showing well with the GridView.

The problem is only with the Report and I could not bind data dynamically to the ReportViewer. Please help me. Thanks.

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = GetDataSet();
    ReportDataSource rds = new ReportDataSource("Orders", ds.Tables[0]);
    ReportViewer1.LocalReport.DataSources.Clear();
    ReportViewer1.LocalReport.DataSources.Add(rds);
    ReportViewer1.LocalReport.Refresh();

    GridView1.DataSource = ds;
    GridView1.DataBind();
}

private DataSet GetDataSet()
{
    var conString = ConfigurationManager.ConnectionStrings["dotnetConnectionString"];
    string strConnString = conString.ConnectionString;

    SqlConnection conn = new SqlConnection(strConnString);
    conn.Open();
    string sql = "Select * FROM Orders";

    SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
    DataSet ds = new DataSet();        
    ad.Fill(ds);

    return ds;
}

ASPX codes are as below:

<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Height="600px" Width="800px">
        <LocalReport ReportPath="Reports\Report.rdlc">
            <DataSources>
                <rsweb:ReportDataSource />
            </DataSources>
        </LocalReport>
    </rsweb:ReportViewer>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
</div>
</form>

Solution

  • I have already solved my problem.

    The problem is you need to add your codes under IsPostBack wrapping.

    if (!Page.IsPostBack)
    {
    //your binding codes here
    }