Asp.Net Reports with Report viewer


Simple demonstration on how to create reports with report viewer in just 5 steps.
Step 1. Create a Web Page Report.aspx in your asp.net web application.
Put the below code between html tag
Report.aspx

<div style="width: 100%">
    <div class="borderbox">
        <table border="0px" style="width: 100%;">
            <tr>
                <td style="width: 80px">Start Date :</td>
                <td style="width: 120px">
                    <asp:TextBox ID="txtStartDate" runat="server" Style="width: 100px"></asp:TextBox>
                    <ajaxtoolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtStartDate">
                    </ajaxtoolkit:CalendarExtender>
                </td>
                <td style="width: 80px">End Date :</td>
                <td style="width: 120px">
                    <asp:TextBox ID="txtEndDate" runat="server" Style="width: 100px"></asp:TextBox>
                    <ajaxtoolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtEndDate">
                    </ajaxtoolkit:CalendarExtender>
                </td>
                <td>
                    <asp:Button ID="btnShow" runat="server" Text="View" OnClick="btnShow_Click" />
                </td>
            </tr>
        </table>
    </div>
</div>
<div style="width: 100%">
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Height="400px" Width="100%"
        Font-Names="Verdana" Font-Size="8pt" InteractiveDeviceInfos="(Collection)" WaitMessageFont-Names="Verdana"
        WaitMessageFont-Size="14pt" PageCountMode="Actual">
    </rsweb:ReportViewer>
</div>

Add the report viewer reference on your page

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

Write the below code on the page load event

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
            txtEndDate.Text = DateTime.Now.ToString("MM/dd/yyyy ");
        }
    }

Write the below code on button click event

protected void btnShow_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            Report objReport = new Report();
            dt = objReport.getReport(txtStartDate.Text, txtEndDate.Text);
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportDataSource _rsource = new ReportDataSource("dsReport", dt);
            ReportViewer1.LocalReport.DataSources.Add(_rsource);
            ReportViewer1.LocalReport.Refresh();
        }
        catch (Exception ex)
        {
        }
    }

Step 2.  Add a data set to your Application.
Give it a name “dsReport”.
Now at the data set interface right click and select data table.
Add columns to data table
Dataset

 

 

 

 

Step 3.  Add a Report Viewer file to your application.

Report.rdlc

 

 

 

 

 

Step 4.  Add data set to Report.rdlc file.
Go to View select report data.
ReportData
AddDataset

 

 

 

 

 

 

 

 

 

 

 

 

Select your data source and available data sets.
DataSource
Step 5. Add matrix to report viewer.
AddMatrixtordlc
Select columns from data set.
AddDatatordlc
Report is ready.
Readyreport

One thought on “Asp.Net Reports with Report viewer

Comments are closed.