Search This Blog

Sunday, February 26, 2012

Creating Sql Server Report (RDLC) using Dataset


Generally we create a strongly typed data-set during design time for creating a RDLC Report which is considered to be data-source for a Report. To achieve this we can create a dataset and populate data and bound to data-source.
In this article we will see how a data-set can provide data to a report-viewer control at run-time. 


How to bind data-set and RDLC report to a Report-viewer at run-time.

Steps:
1. Refer to your defined connection string of web.config.
 in general declaration section   :                                                           string connn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
2. Bind your dataset data to Report-Viewer
// The original dataset will be cleaned and will have new set of data depending upon query output
// Here we want the output to be the employeedetails of city London                                                                                         protected void getData()
    {

        SqlDataAdapter da = new SqlDataAdapter("select * from Employees where city='London'", connn);
        DataSet ds = new DataSet();
        da.Fill(ds);

        ReportDataSource rdc = new ReportDataSource("DataSet1_Employees", ds.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(rdc);
        ReportViewer1.Visible = true;
        ReportViewer1.LocalReport.Refresh();
        
    }
3. Call your getData Method in Page_Load
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           getData();
        }
    }
//Output
Once you get the output in Report-Viewer, can export to PDF or Excel using the red color highlighted dropdown and clicking on export button then.


This is how a dataset and RDLC can render data at run-time.

No comments:

Post a Comment