Click here to Skip to main content
15,887,776 members
Articles / Web Development / ASP.NET
Article

Crystal Reports PUSH method using ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.61/5 (55 votes)
21 Mar 20032 min read 561.4K   80   77
This article explains how to use PUSH method for drawing reports.

Introduction

This article explains how to use PUSH method for drawing reports. It will also explain how to use user DataSets in an ASP.NET page for reports. There are two types of methods for drawing the reports:

  1. PULL method- the crystal report makes connection with the database, brings the fields data and draws the report.
  2. PUSH method- we create the DataSet, choose fields of the DataSet as report fields and then push it to the crystal report. Here I am going to explain the PUSH method only.

Steps

  1. Create a new ASP.NET project.

    Sample screenshot

  2. Insert new item as DataSet.

    Sample screenshot

  3. Add elements to the DataSet which you want on the report. Save All, then right click on the DataSet filename in the solution explorer and select command "Build and browse".
  4. Then add a new item to the project as “Crystal report” and insert a blank report.
  5. In the server explorer, right click database field and select database expert and expand the project data and select DataSet in the table selector and press OK.

    Sample screenshot

  6. Then drag the fields from the database fields of server explorer in the detail section of the report. Arrange the fields as you want.

    From the toolbox, add crystal report viewer control on to the page. That will add this variable:

    C#
    protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
  7. Populate the DataSet. Set the report DataSource and CrystalReportViewer report source.
    C#
    private void Page_Load(object sender, System.EventArgs e)
    {
    CrystalReport1 report=new CrystalReport1();
    CrystalReportViewer1.Visible=true;
    DataSet ds=new DataSet("Account");//give same name as on 
    //dataset1 table header
    DataTable table=new DataTable("Account");//give same name as on 
    //dataset1 table header 
    table.Columns.Add("Fname",typeof(System.String));
    table.Columns.Add("Lname",typeof(System.String));
    table.Columns.Add("Salary",typeof(System.String)); 
    DataRow row=table.NewRow();
    row["Fname"]="Mathew";
    row["Lname"]="Hayden";
    row["Salary"]="5000$";
    // add to table
    table.Rows.Add(row);
    ds.Tables.Add(table);
    // set report's dataset
    report.SetDataSource(ds);
    // set report source
    CrystalReportViewer1.ReportSource =report;
    }

    The output will be as follows...

    Sample screenshot

Points to be careful of

  1. Give same names on Dataset and the table element name of inserted DataSet.
  2. As and when you modify DataSet, build it again and log off the current connections in report. Set the DataSource location again pointing to new DataSet, otherwise database fields of the report will not take the change.
  3. Setting up DataBind properties of the report viewer can be avoided. It can be done at runtime.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Autodesk Inc.
United States United States
my name is omkar bhave. I work in Autodesk Inc. USA.

Comments and Discussions

 
GeneralCreating Report Dynamically Pin
10-Apr-05 22:34
suss10-Apr-05 22:34 
GeneralGood Article Pin
NormDroid6-Apr-05 4:33
professionalNormDroid6-Apr-05 4:33 
GeneralPlugging report with Dataset Pin
Anonymous2-Mar-05 11:14
Anonymous2-Mar-05 11:14 
GeneralCrystal Report in .Net Pin
GeethP1-Mar-05 21:58
GeethP1-Mar-05 21:58 
QuestionHow justify report? Pin
myanonym23-Feb-05 8:50
myanonym23-Feb-05 8:50 
AnswerRe: How justify report? Pin
Anonymous21-Sep-05 14:44
Anonymous21-Sep-05 14:44 
Generalcrystal report sample Pin
Anonymous25-Nov-04 19:48
Anonymous25-Nov-04 19:48 
GeneralManually code the dataset and report bind Pin
cfaulkner1-Nov-04 5:45
cfaulkner1-Nov-04 5:45 
Visual Studio 2003 / SQL 2005 Beta

For some reason I can not use the GUI to create dataset from my stored procedure. It may be because I have multiple selects or it may be SQL 2005. In any case, I must code my dataset by hand. Once done I would think that it would be easy to point my crystal report at this dataset. It not though. I am getting a Logon failed error. If I omit all code and just run the function that creates the dataset I get no error. When should I have a security issue binding the report to a dataset that is in .NET already? Does anyone see anyting wrong with this code?


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ptr
{
///
/// Summary description for availToActualHoursDS.
///

public class availToActualHoursDS : System.Web.UI.Page
{
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

availToActualHoursCR MyReport = new availToActualHoursCR();
CrystalReportViewer1.Visible=true;


// get Logon failed error with and without this code.
CrystalDecisions.Shared.TableLogOnInfo MyLogonInfo;
CrystalReportViewer1.LogOnInfo = new CrystalDecisions.Shared.TableLogOnInfos();
MyLogonInfo = new CrystalDecisions.Shared.TableLogOnInfo();
MyLogonInfo.TableName="MyReport";
MyLogonInfo.ConnectionInfo.UserID = "reporting";
MyLogonInfo.ConnectionInfo.Password = "password";
MyLogonInfo.ConnectionInfo.DatabaseName = "nyce";
CrystalReportViewer1.LogOnInfo.Add(MyLogonInfo);


// set report's dataset
MyReport.SetDataSource(CreateATAHDataSet(2003, 8, 2004, 7, "55200,55300"));
// set report source
CrystalReportViewer1.ReportSource = MyReport;


}

// create a dataset for the cyrstal report to report off of
private DataSet CreateATAHDataSet(int startyearpart, int startmonthpart, int endyearpart, int endmonthpart, string ccntrs)
{
//11-16 Prog ASP.NET - Stored Procedures
string connectionString = "server=cfaulkner; uid=reporting; pwd=password; database=NYCE";
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
connection.Open();

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
command.Connection = connection;

command.CommandText = "sp_rpt_EmpHrsAvailToActualHrsByMonth";
command.CommandType = CommandType.StoredProcedure;

System.Data.SqlClient.SqlParameter param;

param = command.Parameters.Add("@startyearpart", SqlDbType.Int);
param.Direction = ParameterDirection.Input;
param.Value = startyearpart;

param = command.Parameters.Add("@startmonthpart", SqlDbType.Int);
param.Direction = ParameterDirection.Input;
param.Value = startmonthpart;

param = command.Parameters.Add("@endyearpart", SqlDbType.Int);
param.Direction = ParameterDirection.Input;
param.Value = endyearpart;

param = command.Parameters.Add("@endmonthpart", SqlDbType.Int);
param.Direction = ParameterDirection.Input;
param.Value = endmonthpart;

param = command.Parameters.Add("@ccntrs", SqlDbType.VarChar);
param.Direction = ParameterDirection.Input;
param.Value = ccntrs;

SqlDataAdapter dataAdapter = new SqlDataAdapter();

dataAdapter.SelectCommand = command;
dataAdapter.TableMappings.Add("Table", "ATAH");

DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


Thanks for ANY help! I have been racking my brain for days on this.

Craig
GeneralRe: Manually code the dataset and report bind Pin
fixerfrasse14-Nov-04 18:10
fixerfrasse14-Nov-04 18:10 
QuestionIf the report having a sub report, how can I do? Pin
Goldsunline5-Oct-04 20:10
Goldsunline5-Oct-04 20:10 
GeneralGood article Pin
fixerfrasse20-Sep-04 7:51
fixerfrasse20-Sep-04 7:51 
GeneralAll pages in one asp.net Pin
Manish P9-Sep-04 23:11
Manish P9-Sep-04 23:11 
GeneralCrystal Reports: DatasourceException Pin
spn24-Aug-04 0:46
spn24-Aug-04 0:46 
GeneralDisplaying images in Crystal report. Pin
sansav12-Aug-04 23:14
sansav12-Aug-04 23:14 
GeneralUsing more than one datasource. Pin
sansav12-Aug-04 22:39
sansav12-Aug-04 22:39 
Questionincorrect log on parameters - problem? Pin
Member 72717316-Jun-04 10:50
Member 72717316-Jun-04 10:50 
AnswerRe: incorrect log on parameters - problem? Pin
Víctor Sánchez30-Aug-04 23:02
Víctor Sánchez30-Aug-04 23:02 
GeneralReading Ahead Pin
Anonymous20-May-04 9:40
Anonymous20-May-04 9:40 
GeneralRe: Reading Ahead Pin
Anonymous20-May-04 10:30
Anonymous20-May-04 10:30 
GeneralPrinting Reports (ASP.net) Pin
Sarvesvara (BVKS) Dasa28-Mar-04 19:15
Sarvesvara (BVKS) Dasa28-Mar-04 19:15 
GeneralRe: Printing Reports (ASP.net) Pin
sansav12-Aug-04 23:25
sansav12-Aug-04 23:25 
GeneralRe: Printing Reports (ASP.net) Pin
sd_hartono4-Sep-04 22:23
sd_hartono4-Sep-04 22:23 
GeneralRe: Printing Reports (ASP.net) Pin
Anonymous7-Sep-04 19:07
Anonymous7-Sep-04 19:07 
GeneralRe: Printing Reports (ASP.net) Pin
Anonymous21-Dec-04 20:23
Anonymous21-Dec-04 20:23 
GeneralPUSH in runtime Pin
Håkan Nilsson (k)2-Mar-04 23:23
Håkan Nilsson (k)2-Mar-04 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.