Tuesday, February 23, 2010

Crystal Report with Asp.Net – Data Set Method

  • First you need to add a dataset to your project.
    image    image

  • Then Black Screen will be shown. Now you have to add database tables to your datasets. thus if you did not have any database connection yet create one using server explorer.

    image

    You can use either windows authentication or SQL server Authentication. In here I'm using SQL server  authentication. If every things fine you can select the database from the database list. (you can also create databases from here)

      image
  • Then Drag and drop the tables to the dataset designer.

image

  • Now you can design your report but we will do little a little advance thing. Assume you want to get Table1’s Names by giving the Year. Now you want to pass parameter to the query. So you want to add a new method to a table1’s Data Adapter. For that,

image   imageimage

do as above and generate your query using the query designer as follows.

image

  and click finished. Now can see new method is inserted to your table1’s Data Adapter.

image Now you want to design the Crystal report. For that as previous article you can add a new crystal report with blank report option. Now you want to configure the  report to get the data from the data set.

Thus, go to the database explorer and and the data set to the report as follows.

 

image

Finally click ok and design your report by drag and dropping tables fields.  
  image

 

Now you want to code the report.aspx page to load the report.

protected void Page_Load(object sender, EventArgs e)
{

int Year = 12; // you can also get this from query string;

ReportDocument CollReport = new ReportDocument();
string reportPath = Server.MapPath("CrystalReport1.rpt");
CollReport.Load(reportPath);

DataSet1TableAdapters.Table1TableAdapter ta = new WebApplication2.DataSet1TableAdapters.Table1TableAdapter();

DataSet1.Table1DataTable dt = new DataSet1.Table1DataTable();
ta.FillBy(dt, Year);
CollReport.SetDataSource((DataTable)dt);
CrystalReportViewer1.ReportSource = CollReport;
}
Post a Comment