Crystal Report with DataSet
In order to create a crystal report with Dataset, first we have to add new Dataset (.xsd) file.
To add the dataset file Project->Add New Item.
Then select the Dataset file from the “New Item” template.
After adding a dataset, take a Datatable from toolbox and add columns in DataTable as shown in the above figure. You can add multiple columns as per your need, here I added four columns in DataTable i.e. Column1, Column2, Column3, Column4.
The next window looks like the following and you have to select your created Dataset under “Project Data”, and click “Insert Table”, and then click OK.
After inserting the Datatable set the columns of the Datatable in the details section of the crystal report. You can also customize the report by using the toolbox (Ctrl+Alt+x) of the crystal report.
Drag the Crystal Report Viewer from toolbox and drop it into your application form. Here I had also selected a button (Show Details) in order to show the report on the click of button.
If we create or upgrade the crystal report in an application of Visual Studio 2010, then we also have to specify following piece of code in Application Configuration file (app.config)
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
We also have to use the namespace i.e. CrystalDecisions.CrystalReports.Engine in order to create a report dynamically with dataset.
From the code mentioned below, we can access the designation table details from the database and fill that table into DataSet and set the data source of crystalReportViewer.
private void button1_Click(object sender, EventArgs e)
string connectionString = null;
string sql = null;
connectionString = "data source=server name; initial catalog=database name;user
id=your id; password=your password";
cnn = new SqlConnection(connectionString);
sql = "select employee_id as Column1,employee_name as Column2,employee_designation as
Column3,employee_city as Column4 from designation";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet1 ds = new DataSet1();
CrystalReport1 objRpt = new CrystalReport1();
crystalReportViewer1.ReportSource = objRpt;
Now we can see the output of our report by running an application.