Export (0) Print
Expand All

Using the WebForms ReportViewer Control

To view reports deployed to a report server or reports that exist on the local file system, you can use the WebForms ReportViewer control to render them in a Web application.

To use the ReportViewer Control in a Web application

  1. Create a new Microsoft ASP.NET Web Site using either Microsoft Visual C# or Microsoft Visual Basic.

    - Or -

    Open an exiting ASP.NET Web Site and add a new Web Form.

  2. Locate the ScriptManager control in the AJAX Extensions group in the Toolbox window, and drag it to the design surface of the Web form.

    If the Toolbox is not visible, you can access it from the View menu by selecting Toolbox.

  3. Locate the ReportViewer control in the Toolbox and drag it to the design surface below the ScriptManager control.

    The ReportViewer control named reportViewer1 is added to the form.

After the control is added to the form the ReportViewer Tasks smart tag will appear prompting you to select a report. If the report you wish to view has been deployed to a report server select the <Server Report> option from the Choose Report drop-down list. Once the <Server Report> option is selected two additional properties will appear, Report Server Url and Report Path. The Report Server Url is the address to the report server and the Report Path is the full path to the report you want to render.

If you want to view a report in local mode select either the Design a new report option to launch the report designer or select a report that is already part of the existing project. After you have selected a report, be sure to enter the name of the report RDLC file in the ReportPath property of the ReportViewer control. This property appears under the LocalReport node in the Properties pane.

You have the option of hiding one or more of the items on the ReportViewer toolbar when the report is rendered. For example, you can hide the print button. To hide toolbar items, set the following ReportViewer properties to False in the Properties pane.

  • ShowBackButton

  • ShowExportControls

  • ShowFindControls

  • ShowPageNavigationControls

  • ShowPrintButton

  • ShowRefreshButton

  • ShowZoomControl

The following example demonstrates how to render a report that has been deployed to a report server. This example uses the Sales Order Detail report that is included with the AdventureWorks2012 sample reports project.

The example uses integrated Windows Authentication so you first must enable impersonation. To do this insert the following line into the web.config file:

<!-- Web.config file. -->
<identity impersonate="true"/>
NoteNote

Impersonation is disabled by default.

protected void Page_Init(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Set the processing mode for the ReportViewer to Remote
        reportViewer.ProcessingMode = ProcessingMode.Remote;

        ServerReport serverReport = reportViewer.ServerReport;

        // Set the report server URL and report path
        serverReport.ReportServerUrl =
            new Uri("http://<Server Name>/reportserver");
        serverReport.ReportPath =
            "/AdventureWorks Sample Reports/Sales Order Detail";

        // Create the sales order number report parameter
        ReportParameter salesOrderNumber = new ReportParameter();
        salesOrderNumber.Name = "SalesOrderNumber";
        salesOrderNumber.Values.Add("SO43661");

        // Set the report parameters for the report
        reportViewer.ServerReport.SetParameters(
            new ReportParameter[] { salesOrderNumber });
    }
}

The following example demonstrates how to render a report that is part of the Windows application and has not been deployed to a report server.

To add the Sales Order Detail report to a Web Site

  1. Open the Web Site that the report will be added to.

  2. From the Website menu, select Add Existing Item.

  3. Browse to the location where the AdventureWorks Report Samples project is installed.

    The default location is C:\Program Files\Microsoft SQL Server\100\Samples\Reporting Services\Report Samples\AdventureWorks Sample Reports.

  4. Select the Sales Order Detail.rdl file and click the Add button.

    The Sales Order Detail.rdl file should now be part of the project.

  5. Right-click the Sales Order Detail.rdl file in Solution Explorer and select Rename. Rename the report to Sales Order Detail.rdlc and press ENTER.

    If Solution Explorer is not visible, you can open it from the View menu by selecting Solution Explorer.

The following code example will create a dataset for the sales order data and then render the Sales Order Detail report in local mode.

protected void Page_Init(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Set the processing mode for the ReportViewer to Local
        reportViewer.ProcessingMode = ProcessingMode.Local;

        LocalReport localReport = reportViewer.LocalReport;

        localReport.ReportPath = "Sales Order Detail.rdlc";

        DataSet dataset = new DataSet("Sales Order Detail");

        string salesOrderNumber = "SO43661";

        GetSalesOrderData(salesOrderNumber, ref dataset);

        ReportDataSource dsSalesOrder = new ReportDataSource();
        dsSalesOrder.Name = "SalesOrder";
        dsSalesOrder.Value = dataset.Tables["SalesOrder"];

        localReport.DataSources.Add(dsSalesOrder);

        GetSalesOrderDetailData(salesOrderNumber, ref dataset);

        ReportDataSource dsSalesOrderDetail = new ReportDataSource();
        dsSalesOrderDetail.Name = "SalesOrderDetail";
        dsSalesOrderDetail.Value = dataset.Tables["SalesOrderDetail"];

        localReport.DataSources.Add(dsSalesOrderDetail);

        // Create the sales order number report parameter
        ReportParameter rpSalesOrderNumber = new ReportParameter();
        rpSalesOrderNumber.Name = "SalesOrderNumber";
        rpSalesOrderNumber.Values.Add("SO43661");

        // Set the report parameters for the report
        localReport.SetParameters(
            new ReportParameter[] { rpSalesOrderNumber });
    }
}

private void GetSalesOrderData(string salesOrderNumber,
                           ref DataSet dsSalesOrder)
{
    string sqlSalesOrder =
        "SELECT SOH.SalesOrderNumber, S.Name AS Store, " +
        "       SOH.OrderDate, C.FirstName AS SalesFirstName, " +
        "       C.LastName AS SalesLastName, E.Title AS " +
        "       SalesTitle, SOH.PurchaseOrderNumber, " +
        "       SM.Name AS ShipMethod, BA.AddressLine1 " +
        "       AS BillAddress1, BA.AddressLine2 AS " +
        "       BillAddress2, BA.City AS BillCity, " +
        "       BA.PostalCode AS BillPostalCode, BSP.Name " +
        "       AS BillStateProvince, BCR.Name AS " +
        "       BillCountryRegion, SA.AddressLine1 AS " +
        "       ShipAddress1, SA.AddressLine2 AS " +
        "       ShipAddress2, SA.City AS ShipCity, " +
        "       SA.PostalCode AS ShipPostalCode, SSP.Name " +
        "       AS ShipStateProvince, SCR.Name AS " +
        "       ShipCountryRegion, CC.Phone AS CustPhone, " +
        "       CC.FirstName AS CustFirstName, CC.LastName " +
        "       AS CustLastName " +
        "FROM   Person.Address SA INNER JOIN " +
        "       Person.StateProvince SSP ON " +
        "       SA.StateProvinceID = SSP.StateProvinceID " +
        "       INNER JOIN Person.CountryRegion SCR ON " +
        "       SSP.CountryRegionCode = SCR.CountryRegionCode " +
        "       RIGHT OUTER JOIN Sales.SalesOrderHeader SOH " +
        "       LEFT OUTER JOIN  Person.Contact CC ON " +
        "       SOH.ContactID = CC.ContactID LEFT OUTER JOIN" +
        "       Person.Address BA INNER JOIN " +
        "       Person.StateProvince BSP ON " +
        "       BA.StateProvinceID = BSP.StateProvinceID " +
        "       INNER JOIN Person.CountryRegion BCR ON " +
        "       BSP.CountryRegionCode = " +
        "       BCR.CountryRegionCode ON SOH.BillToAddressID " +
        "       = BA.AddressID ON  SA.AddressID = " +
        "       SOH.ShipToAddressID LEFT OUTER JOIN " +
        "       Person.Contact C RIGHT OUTER JOIN " +
        "       HumanResources.Employee E ON C.ContactID = " +
        "       E.ContactID ON SOH.SalesPersonID = " +
        "       E.EmployeeID LEFT OUTER JOIN " +
        "       Purchasing.ShipMethod SM ON SOH.ShipMethodID " +
        "       = SM.ShipMethodID LEFT OUTER JOIN Sales.Store" +
        "        S ON SOH.CustomerID = S.CustomerID " +
        "WHERE  (SOH.SalesOrderNumber = @SalesOrderNumber)";

    SqlConnection connection = new
        SqlConnection("Data Source=(local); " +
                      "Initial Catalog=AdventureWorks; " +
                      "Integrated Security=SSPI");

    SqlCommand command =
        new SqlCommand(sqlSalesOrder, connection);

    command.Parameters.Add(
        new SqlParameter("SalesOrderNumber",
        salesOrderNumber));

    SqlDataAdapter salesOrderAdapter = new
        SqlDataAdapter(command);

    salesOrderAdapter.Fill(dsSalesOrder, "SalesOrder");
}

private void GetSalesOrderDetailData(string salesOrderNumber,
                       ref DataSet dsSalesOrder)
{
    string sqlSalesOrderDetail =
        "SELECT  SOD.SalesOrderDetailID, SOD.OrderQty, " +
        "        SOD.UnitPrice, CASE WHEN " +
        "        SOD.UnitPriceDiscount IS NULL THEN 0 " +
        "        ELSE SOD.UnitPriceDiscount END AS " +
        "        UnitPriceDiscount, SOD.LineTotal, " +
        "        SOD.CarrierTrackingNumber, " +
        "        SOD.SalesOrderID, P.Name, P.ProductNumber " +
        "FROM    Sales.SalesOrderDetail SOD INNER JOIN " +
        "        Production.Product P ON SOD.ProductID = " +
        "        P.ProductID INNER JOIN " +
        "        Sales.SalesOrderHeader SOH ON " +
        "        SOD.SalesOrderID = SOH.SalesOrderID " +
        "WHERE   (SOH.SalesOrderNumber = @SalesOrderNumber) " +
        "ORDER BY SOD.SalesOrderDetailID";

    using (SqlConnection connection = new
        SqlConnection("Data Source=(local); " +
                      "Initial Catalog=AdventureWorks; " +
                      "Integrated Security=SSPI"))
    {

        SqlCommand command =
            new SqlCommand(sqlSalesOrderDetail, connection);

        command.Parameters.Add(
            new SqlParameter("SalesOrderNumber",
            salesOrderNumber));

        SqlDataAdapter salesOrderDetailAdapter = new
            SqlDataAdapter(command);

        salesOrderDetailAdapter.Fill(dsSalesOrder,
            "SalesOrderDetail");
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft