C# .NET - crystal reports - Asked By james vasanth on 27-Mar-09 02:09 AM

how can i create crystal report in code wise

Crystal Reports - Kalit Sikka replied to james vasanth on 27-Mar-09 02:18 AM

How to use Crystal Report in your project?

It was a problem to know how we can print our reports, but I tell you how.

Studio .NET support crystal report and you can use it easily.

Follow Me:

1. Create a new C#.NET or VB.NET windows application project.

2. With using .NET toolbox, design your project as such as this figure

Sample screenshot

3. You need a TextBox, a Button and a CrystalReportViewer objects in your project.

4. Be sure that your SQL Server connection is started, then connect to your SQL Server and use PUBS tables in your project. (If you have don't know how, I told you in my another article named "All you want in ADO.NET")

5. Now generate your dataset object.

6. Right click in your project name in the solution explorer and choose Add New Item, such as this figure.

Sample screenshot

7. Now find crystal report item and add it to your project.

Sample screenshot

8. After you do that, you must configure your report.

Sample screenshot

9. In the standard report creation wizard, select your dataset in the Project Data option. Select AUTHORS table and add it to selected table part.

Sample screenshot

10. In the next step, select the fields you want to print and add it to the right part and click at the FINISH button.

Sample screenshot

11. Form your TOOLBOX; choose the Report Document object from the Component part and apply its setting.

Sample screenshot

Sample screenshot

12. You are ready for coding your project. For Click Event of the button write these codes:

//Filling dataset with Authors table data.

dataAdapter.Fill(myDataSet.authors);

//Setting dataset tables to the crystal report document object.

crystalReport11.SetDataSource(myDataSet.authors);

//Telling to the report viewer, what is my report document.

crystalReportViewer1.ReportSource = crystalReport11;

//Or you can directly send your report to the printer with this line.

//In this case , you don't need a ReportViewer in your project.



Refer: http://www.codeproject.com/KB/cs/Crystal_Report.aspx

Refer: http://www.c-sharpcorner.com/UploadFile/mahesh/CrystalReportsIntroduction11082005014959AM/CrystalReportsIntroduction.aspx

Kalit Sikka replied to james vasanth on 27-Mar-09 02:25 AM

Adding a Report to a Project

Crystal Report is a part of Add New Item templates available in Visual Studio. To add a report to the project, add a new item by Right Clicking on the project in Solution Explorer and selecting Add->Add New Item->Crystal Report as you can see in Figure 1. I change the name of the report to Customers because we will be displaying customers related information from Northwind database.

Now click the Open button.


Figure 1. Adding a Report to the Project

The next step is to select the report document type from Crystal Report Gallery. As you can see from Figure 2, Crystal Report Gallery provides three options Using the Report Expert, As a Blank Report, or From an Existing Report.

For our first application, lets keep all options as default. I will be discussing rest of the options in my following articles.

Figure 2. Selecting a report type.

By clicking the OK button adds Customers.rpt file to the project and launches Standard Report Expert wizard as you can see in Figure 3, where you can select a data source. The Standard Report Expert wizard has 8 different tabs. I will discuss these tabs in more details in my following articles. The main purpose of this article is to show you how to create your first report in a few simple and quick steps.

Figure 3. Standard Report Expert wizard

Adding a Data Source

Now our next step is to link a data source to the wizard. There are different ways to link a data source to the wizard. The simplest way is to click on the OLE DB(ADO) tree item in Figure 3.

Clicking on OLE DB(ADO) item launches OLE DB Provider dialog where you can select a data source as you can see in Figure 4.

Figure 4. OLE DB Provider dialog

Now on the OLE DB Provider, you can select a provider. I select Microsoft OLE DB Provider for SQL Server. If you do not have access to SQL Server, you can select Access or other data providers.

After select Microsoft OLD DB Provider for SQL Server, you will see the Next button is activated on the wizard.

The Next button allows you to provide the connection information about a data source. As you can see in Figure 5, we select Northwind database.

Figure 5. Connection Information dialog

Now you can click the Finish button.

This option adds the Northwind database to the list and if you expand the database item, you will see the database tables.

Now on this page, I select Customers table and add it by clicking Insert Table button. Now you can see the table listed in the right list box. See Figure 6.

Figure 6. Northwind database tables

Now click on Next button which moves to the Fields tab. On this page, you can select what field you want to use in the report. For simplicity, I add only 5 columns to the report CustomerId, CompanyName, ContactName, ContactTitle, and City. See Figure 7. On this page, you can change the heading of a column by selecting a column and changing its name in the Column Heading text box.

Figure 7. Adding columns to report

Now we can continue through different pages but we will stop here. Click Finish here. This option adds the report with all columns to the project. The final report file looks like Figure 8.

Figure 8. Customers.rpt

Adding a Crystal Report Viewer Control

Now our next step is to add Customers.rpt to the Form. For this, we need to make sure the size of the Form is big enough to fit the report. So you may want to go ahead and modify the size of Form.

After resizing the Form, we need to add Crystal Report Viewer from Toolbox to the Form. Simply drag the Crystal Report Viewer control from Toolbox to the Form and resize it accordingly. My final Form looks like Figure 9.

Figure 9. Form with Crystal Report Viewer

Attaching Report to Crystal Report Viewer Control

The last step is to attach Customers.rpt to Crystal Report Viewer control. For this, we need to set the ReportSource property of Crystal Report Viewer control. On the Forms load event handler, we create an Instance of Customers class, and set it as the ReportSource property of Crystal Report Viewer control.

Just write the following two bold lines on your Forms load event handler.

private void Form1_Load(object sender, System.EventArgs e)
{
Customers custReport =
new Customers();
crystalReportViewer1.ReportSource = custReport;
}

Now compile and run the project. The output looks like Figure 10. Now you can see the report with data. With the help of Crystal Report Viewer, you can select from various options such as zoom in, zoom out, print, move next, move last, move previous, and move first.

Figure 10. The final report


You cannot do that. - Sathish S replied to james vasanth on 27-Mar-09 02:31 AM

you can create a crystal report at the design time and pass parameters, values to a crystal report at the runtime. sample code is the link below

http://aspalliance.com/661

TRY THIS - C_A P replied to james vasanth on 27-Mar-09 06:25 AM

Webform1.aspx.cs

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

 

namespace CrystalReportEg

{

          /// <summary>

          /// Summary description for WebForm1.

          /// </summary>

          public class WebForm1 : System.Web.UI.Page

          {

                   protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;

                   protected System.Web.UI.WebControls.Label Label1;

                   protected System.Web.UI.WebControls.TextBox TextBox1;

                   protected System.Web.UI.WebControls.Button Button1;

                   public Customer oRpt = null;

 

                   private void Page_Load(object sender, System.EventArgs e)

                   {

                             // Put user code to initialize the page here

 

                   }

 

                   #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);

                             oRpt = new Customer();

                             GenerateReport();

                   }

                   

                   /// <summary>

                   /// Required method for Designer support - do not modify

                   /// the contents of this method with the code editor.

                   /// </summary>

                   private void InitializeComponent()

                   {    

                             this.Button1.Click += new System.EventHandler(this.Button1_Click);

                             this.Load += new System.EventHandler(this.Page_Load);

 

                   }

                   #endregion

 

                   private void Button1_Click(object sender, System.EventArgs e)

                   {

                             GenerateReport();

                   }

                   protected void GenerateReport()

                   {

                             SqlConnection sqlConn = new SqlConnection("Server=localhost;uid=sa;password=;initial

                                                                      catalog=Northwind;");

                                                         

                             SqlCommand comd;

                             comd = new SqlCommand();

                             comd.Connection = sqlConn;

                             comd.CommandType = CommandType.StoredProcedure;

                             comd.CommandText = "up_GetAllCustomer";

 

                             comd.Parameters.Add("@Companyname",SqlDbType.VarChar,50);

                             if(TextBox1.Text.Trim()!="")

                                      comd.Parameters[0].Value=TextBox1.Text;

                             else

                                      comd.Parameters[0].Value=DBNull.Value;

         

                             SqlDataAdapter sqlAdapter = new SqlDataAdapter();

                             sqlAdapter.SelectCommand = comd;

                            

                             Dataset1 ds = new Dataset1();

                             sqlAdapter.Fill(ds, "Customers");

                            

                             oRpt.SetDataSource (ds);

                                               

                             CrystalReportViewer1.Visible=true;

                   

                             CrystalReportViewer1.ReportSource = oRpt;

                   }

          }

}

 

try this - C_A P replied to james vasanth on 27-Mar-09 06:26 AM
1.) First Create a Dataset. with the expecting table structure.
(For that you should right click on the project name at Solution explorer, then click the "Add new item" and then select the dataset.)
2.)When you are specifying the dataset at the first column you should specify the table name and then specify the field names and data types.
3.)then as above add a new crystal report, At this point select using the report wizad at first pane and statnd at the second pane.
4.)then in the next of the wizard select the last tab called "Creat new connection".
5.) then in the drop down again select ADO.Net (XML) tab. once you select that you can see a small window in that window for the "file path", select the dataset that you have created in the above steps.
6.)The press finish. and in the next step select the table name as your added dataset and move that into the selected tables pane. then press next and in the next pane select the fields that you want display in the report and finally press finish. now you can see the reports with required fields.
7.)then in the web form code window type the following codes.. (This is purely my own coding and name coventions, please make the required changes and run the project.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace SampleDynamicWebReports
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected SampleDynamicWebReports.DataSet1 DataSet1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtFName;
protected System.Web.UI.WebControls.TextBox txtLName;
protected System.Web.UI.WebControls.Button cmdOk;
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox txtComment;
protected System.Web.UI.WebControls.Button Button1;

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

#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);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdOk.Click += new System.EventHandler(this.cmdOk_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Customer");
dt.Columns.Add("FName");
dt.Columns.Add("LName");
dt.Columns.Add("Comments");
DataRow dr = dt.NewRow();
dr["FName"] = txtFName.Text;
dr["LName"] = txtLName.Text;
dr["Comments"] = txtComment.Text;
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}


private void cmdOk_Click(object sender, System.EventArgs e)
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.DataSet1 = new SampleDynamicWebReports.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.DataSet1)).BeginInit();
PageMargins margins;


//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Customer", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("FName", "FName"),
new System.Data.Common.DataColumnMapping("LName", "LName"),
new System.Data.Common.DataColumnMapping("Comments", "Comments")})});
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO [Customer] (FName, LName, Comments) VALUES (@FName, @LName, @Comments); SELECT FName, LName, Comments FR" +
"OM [Customer]";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FName", System.Data.SqlDbType.VarChar, 50, "FName"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@LName", System.Data.SqlDbType.VarChar, 50, "LName"));

//

//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT FName, LName ,Comments FROM [Customer]";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// DataSet1
//
this.DataSet1.DataSetName = "Dataset1";
this.DataSet1.Locale = new System.Globalization.CultureInfo("en-US");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.DataSet1)).EndInit();


Customers rptObj = new Customers();
//this.sqlDataAdapter1.Fill(this.DataSet1);
//rptObj.SetDataSource(this.DataSet1);
rptObj.SetDataSource(this.GetDataSet());
margins = rptObj.PrintOptions.PageMargins;
margins.bottomMargin = 500;
margins.leftMargin = 500;
margins.rightMargin = 500;
margins.topMargin = 500;
// Apply the page margins.
rptObj.PrintOptions.ApplyPageMargins(margins);
rptObj.PrintOptions.PrinterName = "Canon LASER SHOT LBP-1120";
//rptObj.PrintOptions.PrinterName = @"\\192.168.1.125\Canon LASER SHOT LBP-1120";
rptObj.PrintToPrinter(1,false,0,0);
this.CrystalReportViewer1.ReportSource = rptObj;

}
}
}

more info:-
----------
http://www.codetoad.com/forum/18_24170.asp?spage=2
TRY THIS LINK - C_A P replied to james vasanth on 27-Mar-09 06:26 AM

http://aspalliance.com/661_Create_Crystal_Reports_on_the_Fly_using_ASPNET.all

http://www.codeproject.com/KB/database/MySQLCrystal.aspx

http://www.eggheadcafe.com/community/aspnet/2/10077926/dynamic-crystal-report-in.aspx

 

C# Crystal Report - bolton ss replied to james vasanth on 14-Apr-09 06:39 AM

Create a Crystal report using wizard and put this source code

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();

http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-stepbystep.htm

bolt.

thank u - james vasanth replied to bolton ss on 15-Apr-09 11:45 PM
end of post
kumar replied to james vasanth on 07-Oct-10 03:34 PM
hi, this may help you
http://infynet.wordpress.com/2010/10/06/crystal-report-in-c/