ASP.NET - Reportviewer in asp.net - Asked By Anandh Ramanujam on 10-Nov-11 03:19 AM

Hi,

I am going to use report viewer in my application.

Is the report viewer data source should be RDLC files or we can bind RDL files too.

If so, how we can do that. Please help me in this regards.

Thanks
Jitendra Faye replied to Anandh Ramanujam on 10-Nov-11 03:34 AM
Try this code-

using

System.Data;

using

System.Data.SqlClient;

using

System.Configuration;

using

CrystalDecisions.CrystalReports.Engine;

using

CrystalDecisions.Shared;

using

CrystalDecisions.Web;

using

CrystalDecisions.ReportSource;

public

partial class Default2 : System.Web.UI.Page

{


SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);

SqlCommand sqlcmd;

SqlDataAdapter da;

DataTable dt = new DataTable();

string sid,query;

protected void Page_Load(object sender, EventArgs e)

{


if (!Page.IsPostBack)

{

sid = Request.QueryString[

"sid"];

try

{

sqlcon.Open();


//Check whether query string have value or not if not have value then select all record in below query otherwise select particular record with help of where condition

query="Query for getting data from 3 tables";

sqlcmd = new SqlCommand(query,sqlcon);

da = new SqlDataAdapter(sqlcmd);

dt.Clear();

da.Fill(dt);


if (dt.Rows.Count > 0)

{

ReportDocument RptDoc =

new ReportDocument();

RptDoc.Load(Server.MapPath(

"~/CrystalReport.rpt"));

RptDoc.SetDataSource(dt);

CrystalReportViewer1.ReportSource = RptDoc;

CrystalReportViewer1.DataBind();

}

}


catch(Exception ex)

{

Response.Write(ex.ToString());

}


finally

{

sqlcon.Close();

}

}

}

}

Try this and let me know.

Anandh Ramanujam replied to Jitendra Faye on 10-Nov-11 05:35 AM
Thanks for reply...

Im asking about ReportViewer control. Shall we bind the rdl report source to the ReportViewer control?

Thanks.
Jitendra Faye replied to Anandh Ramanujam on 10-Nov-11 05:48 AM
ok,

In Visual studio 2005,there is a report veiwer contrlo to view your RDL reports.


You need to link report viewer control to corresponding report in Reporting service project.This u can do

by setting Propertis of report viewer.

You can also pass parameters form ASP page to rdl file.


using




System;


using


System.Data;


using


System.Configuration;


using


System.Collections;


using


System.Web;


using


System.Web.Security;


using


System.Web.UI;


using


System.Web.UI.WebControls;


using


System.Web.UI.WebControls.WebParts;


using


System.Web.UI.HtmlControls;


public


partial class Default2 : System.Web.UI.Page


{




protected void Page_Load(object sender, EventArgs e)

{




this.ReportViewer2.Visible = false;

}




protected void Button1_Click(object sender, EventArgs e)

{

 




this.ReportViewer2.Visible = true;



this.ReportViewer2.ServerReport.ReportServerUrl = new System.Uri("http://localhost/ReportServer");

 




string strReport = "/ReportProject14/Report1";



this.ReportViewer2.ServerReport.ReportPath = strReport;

Microsoft.Reporting.WebForms.ReportParameter[] RptParameters =




new Microsoft.Reporting.WebForms.ReportParameter[3];



string strComp = this.DropDownList1.SelectedItem.Value;

RptParameters[0] =




new Microsoft.Reporting.WebForms.ReportParameter("INTCOMPANYKEY", strComp);



string strFacility = this.DropDownList3.SelectedItem.Value;

RptParameters[1] =




new Microsoft.Reporting.WebForms.ReportParameter("INTFACILITYKEY", strFacility);



string strActivity = this.DropDownList2.SelectedItem.Value;

RptParameters[2] =




new Microsoft.Reporting.WebForms.ReportParameter("Activity", strActivity);



this.ReportViewer2.ServerReport.SetParameters(RptParameters);



this.ReportViewer2.ServerReport.Refresh();

}

}





Hope this will help you.

kalpana aparnathi replied to Anandh Ramanujam on 10-Nov-11 01:50 PM
Try this Sample code:

<%@ Page Language="C#" %>
 
  <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
 
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
 
    <title>Sample Report</title>
   <script type="text/javascript" src="../../Scripts/jquery-1.4.2.min.js" ></script>
 
    <script type="text/javascript" src="../../Scripts/jquery.layout.js" ></script>
 
    <script type="text/javascript">
 
      $(document).ready(function () {
 
        $('body').layout({
 
          north__paneSelector: "#HeadingPane",
 
          center__paneSelector: "#ContentPane",
 
          resizable: false,
 
          closable: false,
          north__size: 20,
 
          spacing_open: 0,
 
         spacing_closed: 0,
 
          center__onresize: function () {
 
            var reportViewer = $find("ReportViewer");
 
            reportViewer.recalculateLayout();
 
          }
 
        });
 
      });
   </script>
 
  </head>
 
    
 
  <body>
 
  <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
 
    </asp:ScriptManager>
 
    
 
    <div id="HeadingPane">
     Page header content like site navigation goes here
 
    </div>
 
    
  <div id="ContentPane">
   <asp:ObjectDataSource ID="SampleDataSource" runat="server" SelectMethod="GetSampleData"
      TypeName="MyApp.Reports.ReportDataSource">
 
    </asp:ObjectDataSource>
 
     
 
    <rsweb:ReportViewer ID="ReportViewer" runat="server" Font-Names="Verdana"
 
      Font-Size="8pt" Height="100%" Width="100%" ShowBackButton="false">
 
      <LocalReport ReportPath="SampleReport.rdlc">
 
        <DataSources>
 
          <rsweb:ReportDataSource DataSourceId="SampleDataSource" Name="SampleData" />
        </DataSources>
     </LocalReport>
   </rsweb:ReportViewer>
    
 
    </div>
 
    </form>
 
  </body>
 
   </html>