Professional Documents
Culture Documents
From the following pictures you can see some data in the table for C# - Crystal Reports tutorial Order Master Table Data
Now you will get the default Form1.cs. From the main menu in Visual Studio C# project select PROJECT-->Add New Item . Then Add New Item dialogue will appear and select Crystal Reports from the dialogue box.
Accept the default settings and click OK. Next step is to select the appropriate connection to your database (here crstaldb). Here we are going to select OLEDB Connection for SQL Server to connect Crystal Reports in C#. Select OLE DB (ADO) from Create New Connection .
The next screen is the SQL Server authentication screen for connecting to the database - crystalDB. Select your Sql Server name , enter userid , password and select your Database Name .
Click next , Then the screen shows OLE DB Property values , leave it as it is , and then click finish button.
After you click the finish button , the next window you will get your Server name under OLEDB Connection, from there selected database name (Crystaldb) and click the tables , then you can see all your tables from your database. From the tables list double click the Product table then you can see the Product table will come in the right side list.
Click Next Button Select all fields from Product table to the right side list .
Click Finish Button. Then you can see the Crystal Reports designer window in your C# project. In the Crystal Reports designer window you can see the selected fields from Product table. You can arrange the field Objects and design of the screen according your requirements. After that your screen is look like the following picture.
Now the designing part is over and the next step is to call the Crystal Reports in your C# application and view it through Crystal Reports Viewer control in C#. Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .
After you drag the CrystalReportViewer to your form , it will look like the following picture.
Code:
using System;
using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }
The next step is to make relations between these selected tables. Here we are connecting the related fields from each table. For that we arrange the tables in visible area in the list (this is not necessary ) and select the fields that we want to make relation and drag to the related field of the other selected tables. After made the relations with tables the screen is look like the following picture .
Next step is to select the fields from the selected tables ( OrderMaster, OrderDetails, Product) . Here we are selecting the fields Customername , orderdate from ordermastertable , Productname from product table and quantity from order details table. The field selection screen is look like the following picture .
After select the fields from tables, click the Finish button because now we are not using any other functionalities of the Crystal Reports wizard. After that you will get the Crystal Reports designer window You can arrange the fields in the designer window according to your requirement to view the report . For re-arranging fields in the designer window , you can drag the field object on the screen . For editing right click the field object and select Edit Text Object. The following picture shows the sample of designer window after rearrange the field.
Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .
After you drag the CrystalReportViewer to your form , it will look like the following picture.
Here we pass a String parameter from C# to Crystal Reports . For example , from C# program we pass a customer name as a parameter and show all the orders of that customer to the Crystal Reports. In the previous tutorial we saw how to generate a C# - Crystal Reports from multiple tables. This program is the continuation of previous tutorial , the only difference is that we pass a Customer Name as String parameter and get the report of that particular Customer only . Before starting to this section just take a look at the previous section C# Crystal Reports from multiple tables. In the previous section we are getting the report of all orders from all customers , that is , all orders placed by all customers . In this section we pull out the selected customer report only by passing Customer name as argument. Hope you understand the previous section well, if not, please click here C# Crystal Reports from multiple tables. Next step is to create a String parameter in Crystal report designer window . Select the Field Explorer from CrystalReport Menu. (Up to here explained in detail in the previous section C# Crystal Reports from multiple tables)
Then you can see Field Explorer in the Left hand side. Select Parameter Field from Field Explorer and right Click on Parameter Field.
Fill the appropriate name for Name and Prompting text fields like the following picture.
After creating the parameter field , we have to create the selection formula for the Crystal Reports parameter. For creating selection formula , Right click on Crystal Reports designer window , select REPORT -> SELECTION FORMULA -> RECORD .
Now you can see the record Selection Formula Editor Screen. For creating selection formula , you have to select the fields from above field list and make the formula . First you have to select OrderMaster.OrderMaster_customername from Report Field and select the comparison operator and select the parameter field. Double click each field then it will be selected. Form the following picture you can understand how to select fields.
You can close Selection Formula Editor screen after creating selection formula. Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .
Code
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1()
{ InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); ParameterFieldDefinitions crParameterFieldDefinitions ; ParameterFieldDefinition crParameterFieldDefinition ; ParameterValues crParameterValues = new ParameterValues(); ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue(); crParameterDiscreteValue.Value = textBox1.Text; crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; crParameterFieldDefinition = crParameterFieldDefinitions["Customername"]; crParameterValues = crParameterFieldDefinition.CurrentValues; crParameterValues.Clear(); crParameterValues.Add(crParameterDiscreteValue); crParameterFieldDefinition.ApplyCurrentValues(crParameterValues); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }
After creating the parameter field , we have to create the selection formula for the Crystal Reports . For creating selection formula , Right click on Crystal Reports designer window , select REPORT -> SELECTION FORMULA -> RECORD . Then you can see the record Selection Formula Editor. You can make selection formula from this screen by choosing fields from the lists in the selection formula editor screen. Here only the Product table is selected for generating Crystal Reports . We are making a formula like select the records from Product table whose value is greater than the input value. For that, first we select the table field that is Product_price from Product table and then we select the comparison operator and finally we select our Parameter we created earlier. The following picture shows how to select fields from lists . Double click each field then it will automatically selected .
You can close the Selection Formula Editor screen after creating the formula. Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.
The following section describes how to pass a Date parameter field from C# to Crystal Reports. All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#. In the previous sections , we already saw how to pass a string parameter to Crystal Reports - C# Crystal Reports String Paramater, how to pass an Integer parameter to Crystal Reports - C# Crystal Reports Integer Paramater and get the result. This section is very similar to the previous two sections , so before we begin this section , take a look at the previous two sections. C# Crystal Reports String Paramater C# Crystal Reports Integer Paramater When we pass a Date parameter from C# , we have to create a new date parameter in the Parameter Fields of Field Explorer. Then we will get the following screen and fill the fields like in the picture .
After creating the date parameter field , we have to create the selection formula for the Crystal Reports . For creating selection formula , Right click on Crystal Reports designer window , select REPORT -> SELECTION FORMULA -> RECORD . Then you can see the record Selection Formula Editor. You can make selection formula from this screen by choosing fields from the lists in the selection formula editor screen. Here we have three tables selected for report (ordermaster , orderdetails and product ) and we are making the formula like , select all records details from the tables whose order date is greater than the input date parameter. For doing this you have to select Ordermaster.orderdate , a comparison operator and parameter date field from selection list of Formula Editor and make the formula. The following picture shows how to select the fields from formula editor and make the formula. Double click each field then it will automatically selected .
You can close Selection Formula Editor screen after creating the formula. Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; Code
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); ParameterFieldDefinitions crParameterFieldDefinitions ;
ParameterFieldDefinition crParameterFieldDefinition ; ParameterValues crParameterValues = new ParameterValues(); ParameterDiscreteValue ParameterDiscreteValue(); crParameterDiscreteValue.Value = textBox1.Text; crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; crParameterFieldDefinition = crParameterFieldDefinitions["Orderdate"]; crParameterValues = crParameterFieldDefinition.CurrentValues; crParameterValues.Clear(); crParameterValues.Add(crParameterDiscreteValue); crParameterFieldDefinition.ApplyCurrentValues(crParameterValues); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } } crParameterDiscreteValue = new
Code
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); ConnectionInfo crConnectionInfo = new ConnectionInfo(); Tables CrTables ; cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); crConnectionInfo.ServerName = "YOUR SERVER NAME"; crConnectionInfo.DatabaseName = "YOUR DATABASE NAME"; crConnectionInfo.UserID = "YOUR DATABASE USERNAME"; crConnectionInfo.Password = "YOUR DATABASE PASSWORD"; CrTables = cryRpt.Database.Tables ; foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables) { crtableLogoninfo = CrTable.LogOnInfo; crtableLogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplyLogOnInfo(crtableLogoninfo); } crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt You have to replace the source code values of
crConnectionInfo.ServerName = "YOUR SERVER NAME"; crConnectionInfo.DatabaseName = "YOUR DATABASE NAME"; crConnectionInfo.UserID = "YOUR DATABASE USERNAME"; crConnectionInfo.Password = "YOUR DATABASE PASSWORD";
Next step is to create a Formula Field for showing the result of Qty X Price . Right Click the Formula Field in the Field Explorer and click New. Then you will get an Input Message Box , type Total in textbox and click Use Editor.
Now you can see the Formula Editor screen . Here you can enter which formula you want . Here we want the result of Qty X Price . For that we select OrderDetails.Qty , the multipy operator (*) and Product.Price . Double click each field for selection.
Now you can see Total Under the Formula Field . Drag the field in to the Crystal Reports where you want to display Total.
Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
Then you will get a screen , select the Total from the combo box and select Sum from next Combo Box , and summary location Grand Total (Report Footer) . Click Ok button
Now you can see @Total is just below the Total field in the report Footer.
Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; Code
using System;
using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { ReportDocument cryRpt; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } private void button2_Click(object sender, EventArgs e) { try { ExportOptions CrExportOptions ; DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions(); CrDiskFileDestinationOptions.DiskFileName = "c:\\csharp.netinformations.pdf"; CrExportOptions = cryRpt.ExportOptions; { CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; CrExportOptions.FormatOptions = CrFormatTypeOptions; } cryRpt.Export(); } catch (Exception ex) {
MessageBox.Show(ex.ToString()); } } } }
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } private void button2_Click(object sender, EventArgs e) { try { ExportOptions CrExportOptions ; DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); ExcelFormatOptions CrFormatTypeOptions = new ExcelFormatOptions(); CrDiskFileDestinationOptions.DiskFileName = "c:\\csharp.netinformations.xls"; CrExportOptions = cryRpt.ExportOptions; CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; CrExportOptions.ExportFormatType = ExportFormatType.Excel; CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; CrExportOptions.FormatOptions = CrFormatTypeOptions; cryRpt.Export(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
After export the Crystal Reports as a PDF file format in your disk , the next step is to email that .pdf file . For that here we are using System.Web.Mail of C# . We have to provide the necessary information to configuring SmtpMail client and send the exported file as attachment . Select the default form (Form1.cs) you created in C# and drag two buttons (Button1, Button2 ) and a CrystalReportViewer control to your form. You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using System.Web.Mail
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using System.Web.Mail; namespace WindowsApplication1 { public partial class Form1 : Form { ReportDocument cryRpt; string pdfFile = "c:\\csharp.net-informations.pdf"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } private void button2_Click(object sender, EventArgs e) { try { ExportOptions CrExportOptions ; DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); PdfRtfWordFormatOptions(); PdfRtfWordFormatOptions CrFormatTypeOptions = new
CrDiskFileDestinationOptions.DiskFileName = pdfFile;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; CrExportOptions.FormatOptions = CrFormatTypeOptions; cryRpt.Export(); sendmail(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void sendmail() { try { SmtpMail.SmtpServer.Insert(0, "your hostname"); MailMessage Msg = new MailMessage(); Msg.To = "to address here"; Msg.From = "from address here"; Msg.Subject = "Crystal Report Attachment "; Msg.Body = "Crystal Report Attachment "; Msg.Attachments.Add(new MailAttachment(pdfFile)); System.Web.Mail.SmtpMail.Send(Msg); } catch (Exception ex) { MessageBox.Show (ex.ToString()); } } } }
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
Create a new C# Project and create a Dataset from Project - Add New Item Dialogue Box.
Accept the default name DataSet1.xsd . Create a data table for DataSet1.xsd in C#. Select DataSet1.xsd from Solution Explorer and right click . Select datatable from the menu. Then you will get a datatable in the Datast . Right click the datatable and select Add-Column .
Here we are making a two column Crystal Reports , so we need two column in the data table . Add and ID column and Item column in the Data Table.
Now the dataset part is over . Next step is to create a Crystal Reports from the Dataset we created. Select a new Crystal Reports from Add New Item menu and accept the default settings. The next screen is to select appropriate data source . There you can find the Datatable1 from Project data - ADO.NET Datasets , and select Datatable1 to the right side.
Click Next button and select ID and Item from the datatable1 to right side and click finish.
Now the C# Crystal Reports designer part is over . Next part is to create data for the Crystal Reports . For that we have to create a Data Table through programmatically and add data to dataset1. Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using System.Data; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DataSet1 ds = new DataSet1(); DataTable t = ds.Tables.Add("Items"); t.Columns.Add("id", Type.GetType("System.Int32"));
t.Columns.Add("Item", Type.GetType("System.String")); DataRow r ; int i = 0; for (i = 0; i <= 9; i++) { r = t.NewRow(); r["id"] = i; r["Item"] = "Item" + i; t.Rows.Add(r); } CrystalReport1 objRpt = new CrystalReport1(); objRpt.SetDataSource(ds.Tables[1]); crystalReportViewer1.ReportSource = objRpt; crystalReportViewer1.Refresh(); } } }
Create a new Crystal Report and select DataTable as Data Source . You can select DataTable from the wizard , Project Data - ADO.NET Dataset - Crystal report Dataset1 - dataset1. Click the Next Button.
Select fields ( Product_id , Product_name , Product_price ) from the next screen and click the finish button. Then you will get the designer screen with the selected fields. If you do not know how to create a dataset , refer the previous section How to create a C# Crystal Report from Strongly Typed Dataset. Now the designing part is over. From the source code we can pass the SQL source code to Crystal Reports. Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using System.Data; using System.Data.SqlClient ; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1()
{ InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection cnn ; string connectionString = null; string sql = null; connectionString = "data catalog=DATABASENAME;user id=USERNAME;password=PASSWORD;"; cnn = new SqlConnection(connectionString); cnn.Open(); sql = "SELECT Product_id,Product_name,Product_price FROM Product"; SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn); DataSet1 ds = new DataSet1(); dscmd.Fill(ds, "Product"); MessageBox.Show (ds.Tables[1].Rows.Count.ToString()); cnn.Close(); CrystalReport1 objRpt = new CrystalReport1(); objRpt.SetDataSource(ds.Tables[1]); crystalReportViewer1.ReportSource = objRpt; crystalReportViewer1.Refresh(); } } } source=SERVERNAME;initial
connectionString = "data source=SERVERNAME;initial catalog=DATABASENAME;user id=USERNAME;password=PASSWORD;"; You have to provide the necessary database information to Connection String.
All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#. Here we are going to create a Dynamic Crystal Report with the help of Strongly Typed Dataset. Create a new CSharp project and add a Strongly Typed Dataset in the C# Project . Before creating a Strongly Typed Dataset take a look at the detailed tutorial of how to create a C# Crystal Report from Strongly Typed Dataset of previous section . Hope you understand how to create a C# Crystal Report from Strongly Typed Dataset. Create a new C# project and add a Strongly Typed Datset . Add five columns in the DataTable of Strongly Typed Dataset. Here we are limiting as five column , but you can add any number of column according to your reports requirements.
Next step is to create a Crystal Reports design from the Strongly Typed dataset. You have to add a Crystal Report in your project and select data source as Strongly Typed Dataset. If you don't know how to do this section , refer the previous section of How to create a C# Crystal Report from Strongly Typed Dataset.
Click finish button . Then you can see the selected fields in the Crystal Repots . Arrange the fields according to your requirement . Now the designing part is over and the next step is to call the Crystal Reports in CSharp and view it in Crystal Reports Viewer control .
Select the default form (Form1.cs) you created in CSharp and drag a Textbox , button and CrystalReportViewer control to your form (like in the first picture ). Here we are going to pass the SQl statements to Crystal Reports at runtime from C# program . For that we have to parse the SQL statement before we passing it to Crystal Reports. So we create a function for parsing SQL statements in the C# program. Public Function procesSQL() As String You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using System.Data; using System.Data.SqlClient ; namespace WindowsApplication1 { public partial class Form1 : Form { CrystalReport1 objRpt = new CrystalReport1(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection cnn ; string connectionString = null; string sql = null; connectionString = "data source=SERVER catalog=crystaldb;user id=USER NAME;password=PASSWORD;"; cnn = new SqlConnection(connectionString); cnn.Open(); sql = procesSQL(); SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn); DataSet1 ds = new DataSet1(); dscmd.Fill(ds, "Product"); objRpt.SetDataSource(ds.Tables[1]); crystalReportViewer1.ReportSource = objRpt; crystalReportViewer1.Refresh(); } NAME;initial
public string procesSQL() { string sql = null; string inSql = null; string firstPart = null; string lastPart = null; int selectStart = 0; int fromStart = 0; string[] fields = null; string[] sep = { "," }; int i = 0; TextObject MyText ; inSql = textBox1.Text; inSql = inSql.ToUpper(); selectStart = inSql.IndexOf("SELECT"); fromStart = inSql.IndexOf("FROM"); selectStart = selectStart + 6; firstPart = inSql.Substring(selectStart, (fromStart - selectStart)); lastPart = inSql.Substring(fromStart, inSql.Length - fromStart); fields = firstPart.Split(','); firstPart = ""; for (i = 0; i <= fields.Length - 1; i++) { if (i > 0) { COLUMN" + (i + 1); firstPart = firstPart + ", " + fields[i].ToString() + " AS firstPart.Trim(); MyText = (TextObject) objRpt.ReportDefinition.ReportObjects[i+1]; MyText.Text = fields[i].ToString(); } else { firstPart = firstPart + fields[i].ToString() + " AS COLUMN" + (i + 1); firstPart.Trim(); MyText = (TextObject)objRpt.ReportDefinition.ReportObjects[i+1]; MyText.Text = fields[i].ToString(); }
Download Product.XML Generating Crystal Report from XML file is very similar to generating Crystal Report from Databases. The only difference is happened when you selecting the datasource part. Here you have to select Create New Connection - Database Files and select the XML file you want to generate Crystal Reports (In this case you select the Product.xml ).
Select all the fields from Product and click finish button Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {
ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }
NOTES: cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
Here we are going create an order report based on three tables in the database and show a subreoprt for each row field of Product Name. Here we are using our earlier program C# Crystal Reports from multiple tables to show the main Crystal Reports Data , so refer C# Crystal Reports from multiple tables , before start this section . Create a Crystal Reports using three tables and select customername , date , product and qty . It will explain in detail the previous section C# Crystal Reports from multiple tables. Next step is to create a sub report inside the main report. Here we are showing the Product details in each row of the specified product in the main row. After create the main report , right click on Crystal Reports designer window and select Insert-Subreport.
Then you will get the subreport object , drag the object in the designer window at the down part of the details tab , just below the fields in the details tab. When you release the mouse you will get a dialogue box asking report name . Enter a report name , and click the Report Wizard Button . The wizard shows the table selection screen and select the table . Here in this case we have to select the Product Table from the list and click next . Next screen is showing the table , from there select the fields you want to show the data and click finish. The you will get the sub report main screen again and select Link tab . The link tab is making relation with your main Report and subreport . Here we are linking the productname from main report to the subreport. For that select Product.Product_name from Available fields.
Accept the other settings as it is in the screen and click ok. Now you can see the sub report object in the screen , if you want to re-arrange subreport design , double click on subreport object then you can design subreport.
Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }
This section is the continuation of the previous section C# Crystal Reports sub-reports. Here we are creating a Sub report in Crystal Report and make a link in the main Crystal Reports for on-demand subreport. Select the subreport object in the Crystal Reports and right click , then select Format Object .
Then you will get the Format Editor . Select Sub report tab from Format Editor , you can find there a check box - On-demand Subreport . You have to select that check box , then the sub report become as a link in your main Crystal Reports. If you want to change the title , you can change it in subreport name textbox. Finally click OK button.
Now the designing part is over and the next step is to call the Crystal Reports in CSharp and view it in Crystal Reports Viewer control . Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh();
} } }
Date To Parameter
Here we have three tables selected for report (ordermaster , orderdetails and product ) and we are making the formula like , select all records from the tables whose order date is between fromDate parameter and toDate paramater . For doing this you have to select from date as Ordermaster.orderdate, to date as Ordermaster.orderdate , comparison operators , boolean operator (AND) and date parameters fromdate and todate fields from selection list of Formula Editor and make the formula. The following picture shows how to select the fields from formula editor and make the formula. Double click each field then it will automatically selected .
After the creation of selection formula you can close that screen . Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control . Select the default form (Form1.cs) you created in CSharp and drag two Textboxs (from date entry and to date entry) , a button and CrystalReportViewer control to your form.
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); ConnectionInfo crConnectionInfo = new ConnectionInfo(); Tables CrTables; ParameterFieldDefinitions crParameterFieldDefinitions ; ParameterFieldDefinition crParameterFieldDefinition ; ParameterValues crParameterValues = new ParameterValues(); ParameterDiscreteValue ParameterDiscreteValue(); crParameterDiscreteValue.Value = textBox1.Text; crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; crParameterFieldDefinition = crParameterFieldDefinitions["fromDate"]; crParameterValues = crParameterFieldDefinition.CurrentValues; crParameterValues.Clear(); crParameterValues.Add(crParameterDiscreteValue); crParameterFieldDefinition.ApplyCurrentValues(crParameterValues); crParameterDiscreteValue.Value = textBox2.Text; crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; crParameterFieldDefinition = crParameterFieldDefinitions["toDate"]; crParameterValues = crParameterFieldDefinition.CurrentValues; crParameterValues.Add(crParameterDiscreteValue); crParameterFieldDefinition.ApplyCurrentValues(crParameterValues); crConnectionInfo.ServerName = "YOUR SERVERNAME"; crParameterDiscreteValue = new
crConnectionInfo.DatabaseName = "DATABASE NAME"; crConnectionInfo.UserID = "USERID"; crConnectionInfo.Password = "PASSWORD"; CrTables = cryRpt.Database.Tables; CrTables) { crtableLogoninfo = CrTable.LogOnInfo; crtableLogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplyLogOnInfo(crtableLogoninfo); } crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } } foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
crConnectionInfo.ServerName = "YOUR SERVERNAME"; crConnectionInfo.DatabaseName = "DATABASE NAME"; crConnectionInfo.UserID = "USERID"; crConnectionInfo.Password = "PASSWORD";
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); ConnectionInfo crConnectionInfo = new ConnectionInfo(); Tables CrTables; crConnectionInfo.ServerName = "YOUR SERVERNAME"; crConnectionInfo.DatabaseName = "DATABASE NAME"; crConnectionInfo.UserID = "USERID"; crConnectionInfo.Password = "PASSWORD"; CrTables = cryRpt.Database.Tables; CrTables) { crtableLogoninfo = CrTable.LogOnInfo; crtableLogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplyLogOnInfo(crtableLogoninfo); } cryRpt.Refresh(); cryRpt.PrintToPrinter(2, true, 1, 2); } } } foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
crConnectionInfo.ServerName = "YOUR SERVERNAME"; crConnectionInfo.DatabaseName = "DATABASE NAME"; crConnectionInfo.UserID = "USERID"; crConnectionInfo.Password = "PASSWORD";
Crystal Reports for Visual Studio ships with your deployment projects that enable you to deploy Crystal Reports components and assemblies on the target machine. We can use different approaches to install Crystal Reports runtime files on the target machine in order to run your C# applications smoothly. If you are using Visual Studio then during your setup and deployment you can add the CRRedist2005_x86.msi file to your setup file and distribute it as a single setup file with your C# application. The installer can execute the setup file automatically within your .Net Project in a single click. You can find the CRRedist2005_x86.msi file in your system's C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages\CrystalReports . Also you can distribute the CRRedist2005_x86.msi separately and install it on the target machine where your C# applications installed. The other way to install Crystal Reports on target machine is to create a setup file using Merge Modules and distribute it with your C# application or as a separate setup file. Click the following link to see how to make a setup file with Merge Modules.
Steps to create Setup file for your C# Application. 1) Make sure the above mentioned files are in the \Program Files\Common Files\Merge Modules folder , If the files are not there you can download the file and copy it to the folder \Program Files\Common Files\Merge Modules . Download: https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/11688 2) Open your Visual Studio 3) Select "New Project" from File menu.
4) Select "Setup and Deployment" from "Other Project Types" 5) In the "Solution Explorer", select your setup project, right-click, and select Add and Merge Module from the menu.
6) Select CrystalReportsRedist2005_x86.msm to your setup project. The file available at \Program Files\Common Files\Merge Modules
Then you can see Microsoft_VC80_ATL_x86.msm and policy_8_0_Microsoft_VC80_ATL_x86.msm will be automatically added in the Detected Decencies section. 7) Build your project. Now you will get the setup file for distribution. Either you can add this setup file to your C# application installer or distribute it as a separate setup file.