A Crystal Report with DataSet
In order to create a crystal report with Dataset, first, we have to add a 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 the 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 the 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 a button.
If we create or upgrade the crystal report in an application of Visual Studio 2010, then we also have to specify
the following piece of code in Application Configuration File (app.config)
<configuration>
<startupuseLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
We also have to use the namespace i.e. CrystalDecisions. CrystalReports. The engine in order to create a report dynamically with the dataset.
using CrystalDecisions.CrystalReports.Engine;
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 the crystal report viewer.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cnn;
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);
cnn.Open();
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);
cnn.Close();
DataSet1 ds = new DataSet1();
dscmd.Fill(ds, "DataTable1");
CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(ds.Tables[0]);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();
}
Now we can see the output of our report by running an application.
aadhi k
04-Jun-2017chetna balani
04-Nov-2014Kenny Tangnde
11-Oct-2011nice article.
thanks.