ADO/ADO.NET - How can I create ReportViewer report using DataView datas

Asked By Rahim Huseyn on 27-Jun-12 08:46 AM
How can I create ReportViewer report using DataView datas? And instead of foreign key values I want to show another field from parent table.

I have a spesific problem. Typed DataSet - dtParent, dtChild
dtParent - pid,  field1Parent, field2Parent
dtChild - id, pid, fieldsome1, fieldsome2

DataView dv = new DataView(dtChild);
dv.Rowfilter = "fieldsom1 = 5"; // for example

Question is: How can I create report (ReportViewer) based on dv?
          And I want to see report like below
        id, field1Parent, fieldsome1, fieldsome2

Note: I'm afraid to add extra fields to my Dataset schema, because my select, update, delete, insert commands works very well.
Jitendra Faye replied to Rahim Huseyn on 27-Jun-12 08:50 AM
For crystal report you have to use following namespaces-

using CrystalDecisions.CrystalReports.Engine
using CrystalDecisions.Shared
using CrystalDecisions.Web
using CrystalDecisions.ReportSource



after that use 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();
    }   
  }
  }
}
Hope this will help you

TSN ... replied to Rahim Huseyn on 27-Jun-12 09:30 AM

Hi…

 

Here is the sample Example that shows how to bind your Data to the Report Viewer Control from multiple Table

write the below code in Button Click event where you want to generate the Report

 

// render via the ReportViewer control
reportViewer.ProcessingMode = ProcessingMode.Local;

// set report
reportViewer.LocalReport.ReportPath = "MyReport.rdlc";

 

//Here add a Connection to the SqlDatabase and try to get the Data into The Dataset

string connectionString = "Data Source=localhost;Initial Catalog=DatabaseName;"

      + "Integrated Security=true;";

//here is the Query to get the data..

string Query= ” select dp.field1Parent , dc.fieldsome1 , dc.fieldsome2 from dtParent dp  join dtChild dc on dp.Pid=dc.Pid

SqlDataAdapter da = new SqlDataAdapter(Query,Conn)

da.Fill(ds)

 

// set data
DataTable dt =
ds.Tables[ 0 ];

//Binding Data to report viewer
reportViewer.LocalReport.DataSources.Add( new ReportDataSource( "MyDataSet", dt ) );

// render report
reportViewer.RefreshReport();

 

note: Here you are not using any Dataview and You need not change any change from Dataview

Rahim Huseyn replied to TSN ... on 27-Jun-12 09:38 AM
Ok. Thanks very much.
I have another little question. I don't want to connect to database every time when printing.
Is it good practice to use LINQ to ceate Datatable for print?
TSN ... replied to Rahim Huseyn on 27-Jun-12 09:51 AM
hi...

If you are using Linq then its well and good , you can do it.

No issues in doing either way.


Rahim Huseyn replied to TSN ... on 27-Jun-12 10:16 AM
Thanks