SSRSControl.aspx <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SSRSControl.aspx.cs" Inherits="WebFormDemo.SSRSControl" EnableSessionState="True" %>
SSRSControl.aspx.cs using Microsoft.Reporting.WebForms; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebFormDemo { public partial class SSRSControl : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = "select * from Customers"; if (Request.QueryString["id"] != null && Request.QueryString["id"] != string.Empty) { query = query + " where CustomerId=" + Request.QueryString["id"]; } ReportViewer1.ProcessingMode = ProcessingMode.Local; ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc"); Customers dsCustomers = GetData(query); ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]); ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.DataSources.Add(datasource); } } private Customers GetData(string query) { string conString = ConfigurationManager.ConnectionStrings["conStr"].ToString(); SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (Customers dsCustomers = new Customers()) { sda.Fill(dsCustomers, "DataTable1"); return dsCustomers; } } } } } } Index.cshtml (MVC project) @{ ViewBag.Title = "Home Page"; }