C# Crystal Reports without database - IJCSMS



A step by step tutorial for beginners who is creating their Crystal Reports for the first time in C#. 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# crystaldbHere we are going to create a new Crystal Reports in C# from Product table in the above mentioned database crystalDB. The Product Table has three fields (Product_id,Product_name,Product_price) and we are showing the whole data from Product table to the C# - Crystal Reports project.Open Visual Studio .NET and select a new C# Windows project.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.Select Report type from Crystal Reports gallery.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 .Select Microsoft OLE DB Provider for SQL Server .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 ButtonSelect 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.You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.using CrystalDecisions.CrystalReports.Engine; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports from multiple tables Download Source CodePrint Source Codeusing 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(); } }}NOTES: cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project file location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt When you run the source code you will get the report like the following picture. When you click the button, the application will ask the username and password. Later in this tutorial you can find how to avoid asking username and password - C# Dynamic logon parameters in Crystal Reports. Hope this tutorial help you to create your first Crystal Reports in C#.Crystal Report From Multiple Table DataThe following section describes how to create Crystal Report from multiple tables in C#.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# crystaldbHere we are going to generate Crystal Reports from multiple tables in C#. Here we have three tables (ordermaster , orderdetails and product ) and we are generating a Crystal Report from these three tables by connecting each table with their related fields.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#.Hope you understand the basics of generating a Crystal Reports in C# , this section is the continuation of the first part, so here we avoid some basic steps and start from the table selection of Crystal Reports.Select all table from the table list to right side list box, because we are creating report from three tables ( OrderMaster, OrderDetails, Product) . If you don't know up to this part of the tutorial , refer previous tutorial for up to selecting databese for Crystal reports.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.You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.using CrystalDecisions.CrystalReports.Engine; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports String parameter Download Source CodePrint Source Codeusing 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(); } }}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 After you run the source code you will get the report like this. When you click the button, the application will ask the username and password. Later in this tutorial you can find how to avoid asking username and password - C# Dynamic logon parameters in Crystal Reports.The following section describes how to pass a String parameter to Crystal Reports from C# application.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# crystaldbIf 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 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 .You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports Integer parameter Download Source CodePrint Source Codeusing 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(); } }}NOTE : 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 Now you can run the program . Enter a Customer Name(enter any existing customer from Ordermaster table) and click the button , then your report is look like the following picture. Here we get the result of the Customer4's order details.C# CRYSTAL REPORT STRING PARAMETERThe following section describes how to pass a String parameter to Crystal Reports from C# application.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# crystaldbIf 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 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 .You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports Integer parameter Download Source CodePrint Source Codeusing 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(); } }}NOTE : 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 Now you can run the program . Enter a Customer Name(enter any existing customer from Ordermaster table) and click the button , then your report is look like the following picture. Here we get the result of the Customer4's order details.CRYSTAL REPORT WITH INTEGER PARAMETERThe following section describes the how to pass an Integer Parameter to Crystal Reports from C#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# crystaldbIf 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 tutorial , we already saw how to pass a String parameter to the Crystal Reports from C# - C# Crystal Reports String Paramater - and get the result. This section is almost same as the previous section and the only difference is that instead of String parameter here we pass an Integer parameter from C# . So before we start this section you can take look at the previous section C# Crystal Reports String Paramater , it explain each steps in details with pictures.When we pass an Integer parameter , we have to create a new Integer parameter in the Parameter Fields of Field Explorer. Then we will get the following screen and fill the fields like in the following picture .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.using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports Date parameter Download Source CodePrint Source Codeusing 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 = Convert.ToInt32(textBox1.Text); crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; crParameterFieldDefinition = crParameterFieldDefinitions["Price"]; crParameterValues = crParameterFieldDefinition.CurrentValues; crParameterValues.Clear(); crParameterValues.Add(crParameterDiscreteValue); crParameterFieldDefinition.ApplyCurrentValues(crParameterValues); 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 Now you can run the program . Enter any price , then you can see the report of the Product list whose price is greater than or equal to the entered price. The Crystal Reports showing the result of Product list whose price is greater than 50.C# Crystal Reports Dynamic Logon parameters The following section describes how to pass the logon information like Server Name , database Name , User Name and password dynamically to the Crystal Reports from C# applications.When you run the previous programs in this C# tutorial , the Crystal Reports asks the Username and Password every time you run the Crystal Report. This section explains how to avoid the dialogue box asking username and password at runtime on Crystal Reports login . In this section we are passing User ID , Password , Server Name and Database Name dynamically to Crystal Reports from C# .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# crystaldbIf 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 many situations dynamically passing the logon parameter fields are very useful , for example if you develop a database project in a test server environment and later you have to migrate it to other server , in these situations it is better to give these information dynamically to Crystal Reports .In step by step tutorial for creating a Crystal Reports from C# - we created a report selecting all data from the Product table . There is no chance to change the server name or any other information on runtime in that case, because it is a static report and it will asks username and password every time you run the Crystal Reports . Here we are passing Server Name , UserID and Password dynamically to the Crystal Reports from our C# programHere we use Crystal Reports ConnectionInfo class for passing connection information.ConnectionInfo crConnectionInfo = new ConnectionInfo(); 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; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports Formula Field Download Source CodePrint Source Codeusing 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";with your real-time values . Crystal Report With Formula FieldsThe following C# - Crystal Reports section describes how to add a formula field in the 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# crystaldbIf 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#.The situations when formula field is using :If you have a Crystal Reports with Qty and Price fields and you need an additional field in your Crystal Reports for Total, that is TOTAL = QTY X PRICE . In these types of situations you can use the Formula Field in Crystal Reports.In this C# Crystal Reports tutorial we are showing that all orders with qty and price and the total of each row , that means each in each row we are showing the total of qty and price.Before starting this tutorial Create a new Crystal Reports with fields CustomerName , Order Date , Product Name and Product Price . If you do not know how to create this report , just look the previous tutorial C# Crystal Reports from multiple tables . In that report selecting only four fields , here we need one more field Product->Price and one formula field Total.After you create the above Crystal Reports, your CR designer screen is look like the following picture :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; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports Summary Field Download Source CodePrint Source Codeusing 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 When you run this program you will get your screen like the following picture. C# Crystal Reports without database Usually we are using Crystal Reports to fetch data from databases and show it as a report. Here we are generating a Crystal Reports from C# without using a database . For generating a Crystal Reports without database , here we are using a Strongly Typed Dataset and a Data Table of C#.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#.Generating a Strongly Typed DataSet Create a new C# Project and create a Dataset from Project - Add New Item Dialogue Box.Select Dataset from listAccept 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 - 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 .Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports from SQL Query Download Source CodePrint Source Codeusing 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(); } }}C# Crystal Reports from SQL QueryThe following section describes how to create a Crystal Reports in C# using SQL Query 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# crystaldbIf 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#.Generating a Strongly Typed DataSet Here we are using a Strongly Typed Dataset for generating a Crystal Reports from SQL Query string . Before start this section , you can take look at the previous section of how to create a C# Crystal Report from Strongly Typed Dataset. Because here we are using Strongly Typed Dataset for generating Crystal Report from SQL Query string.Here we are generating a report against the Product table . So we are passing the following sql and generating the reportsql = "SELECT Product_id,Product_name,Product_price FROM Product"; Create a new C# project and create a new Strongly Typed Dataset from Project - Add New Item Dialogue Box.Add three column in the Strongly Typed Dataset . Product_id Product_name Product_priceCreate a new Crystal Report and select DataTable as Data Source . You can select DataTable from the wizard , Project Data - 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; Copy and paste the following source code and run your C# project Next?:??Dynamic Crystal Reports from C# Application Download Source CodePrint Source Codeusing 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 source=SERVERNAME;initial 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(); } }}connectionString = "data source=SERVERNAME;initial catalog=DATABASENAME;user id=USERNAME;password=PASSWORD;"; You have to provide the necessary database information to Connection String.Dynamic Crystal Reports from C# ApplicationUsually Crystal Reports generating with pre-defined columns . That is, the fields showing in Crystal Reports are selecting through a wizard at the time of Crystal Report design.The following program describes how to generate a Crystal Report in C# with dynamic columns at the time of report generation . That is, we are not selecting any column from database table at the time of Crystal Report design. Instead of that we are passing an SQl query string and get the Crystal Report dynamically at runtime in C# . From the following picture you can understand how it works.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# crystaldbIf 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.Select all the column (five) 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; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports from XML Download Source CodePrint Source Codeusing 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 NAME;initial 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(); } 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) { firstPart = firstPart + ", " + fields[i].ToString() + " AS COLUMN" + (i + 1); 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(); } } sql = "SELECT " + firstPart + " " + lastPart; return sql; } }}connectionString = "data source=SERVER NAME;initial catalog=crystaldb;user id=USER NAME;password=PASSWORD;"; You have to provide the necessary database information to Connection String.C# Crystal Reports - sub reports The following progrm describes how to create a sub report within a Crystal Report in C#.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# crystaldbIf 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#.Subreport in Crystal Reports means that a report inside a Crystal Report . When we want to show some additional information about a data field in a Crystal Reports row, we use the subreports features to show the details. We can display sub report in two ways , one is to show subreport directly under the main row details. The other method is to show it as C# Crystal Reports on demand sub-reports , that means we put an Hyper link just below the row filed , and when the user click that hyper link it will show the subreport.In this section we are going to generate a sub-report under the main row details. That is for each row there is a subreoprt for the details.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; Copy and paste the following source code and run your C# project Next?:??C# Crystal Reports - on demand sub reports Download Source CodePrint Source Codeusing 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(); } }}How to deploy Crystal Report The following section describes how to deploy a Crystal Reports components and assemblies on the target machine.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#.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.C# Crystal Reports installer using Merge Modules.Next?:??How to Crystal Report Merge Module ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download