Friday, May 9, 2014

Working with Rdlc Reports using Web Application.

Design the Form as following


Default.aspx

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="783px"></rsweb:ReportViewer>
 </div>

 </form>

Go to Visual Studio.

Add New Item




Select Dataset and Give the Name to Dataset.




Dataset will be displayed as below



Right Click On Dataset  and select

Add->Data Table







And Add the Columns.



Write the following Code in Default.aspx.cs

if (!IsPostBack)
        {
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
            Products dsCustomers = GetData("select  ID,ProductCode,ContractDescriptionURL,ProductAvailableForSale from TblProducts");
            ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(datasource);
            ReportViewer1.DataBind();
        }





Write the following Method in Default.aspx.cs Page


private Products GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["testConnect"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (Products dsCustomers = new Products())
                {

                    sda.Fill(dsCustomers, "DataTable1");
                    return dsCustomers;
                }
            }
        }
    }



Again, Go to Visual Studio and Select 
Add->New Item.




And Click on Add Button.




From the Tool Box select Table.





The following screen will be displayed on Selecting the Table.




Select the Data Source-> Products( this name has been given to Dataset)








And click on Ok Button. The below screen will be visible.





Now, go to Data and give the column name.








Run the Program , and see the output.




Note: In case if u get any error as
  • A data source instance has not been supplied for the data source 'DataSet1'.


To solve this Error:

ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);

Check whether these name should be same as Data Source Name.


No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...