C# Reporting data-CrystalReport

CrystalReport



Visual Studio 2008 comes with a useful reporting tool--Crystal Report that enables you to generate reports to show your data retrieved from a database. In this page we will discuss about how to get data stored in a table of a Microsoft SQL SERVER database to display on a form of your project.

Suppose that you want to create a report to display the data from TblProduct of SaleAndStock database. This database exists in Microsoft SQL SERVER(in my machine, i have SQL SEVER 2005). The user that has a right to login to the database is called sa and its password is 123. This login information is useful when we connect the database using C# code. The steps below will help you to get thing done.

-Step 1: Add a DataSet to the project

+Now create a new project in Visual Studio 2008. In my case, i created a project called ReportVB. Ricght-click the project name->Add->New Item. Under Categories, select Data and under Templates select Dataset.

Adding dataset to a project  

+On the Server Explorer next to the active dataset window, click Connect to Database icon to add a connection to the database server. On the Add Connection dialog, click Change button of the Data source block to show the Change Data Source dialog.

Add data connection to SQL server database

+Then on the Change Data Source dialog, select Microsoft SQL Server. After that you will see a new Add Connection dialog that shows the Server name box. In this box, you need to enter the name of your SQL SERVER. In my case, this name is Dcc-pc.

+In the Log on to the server block, select Use SQL Server Authentication. Then type the user name and password in to the User name box and Password box.

+In the Connection to a database, select Select or enter a database name option. Then click the dropdown list to select SaleAndStock database( a sample database used in this tutorial).

Login to the database in Microsoft SQL Server  

+Click Test Connection button to test the connection to the SQL SERVER. If there is a dialog showing that the Connection is succeed, it means that you can move to the next step. If an error occurs you need to check what you have provided again.

-Step 2: Add the TblProduct table to the dataset

After you connected successfully to the SQL Server database, look Server Explorer again and click Data Connection to expand it then select the connection that you have created in the previous step. Then click Tables to expand and drag the TblProduct table to the dataset designer window.

Adding table to the dataset  

-Step 3: Add a Crystal Report and link to the table of dataset

Now you have a schema of a dataset that connects to the TblProduct of the SaleAndStock database located in SQL Server. The next step is to add a Crystal Report item in to your project.

+From the Project menu, click Add New Item.... On the New Item dialog, under Categories select Reporting and under Templates select Crystal Report. You will see the Crystal Reports Gallery dialog. Select As a Blank Report to create a blank report and accept the default name(CrystalReport1).

Add Crystal Report to the project

+To link to the table of the dataset, right-click Field Explorer next to the active window of CrystalReport1 then select Database Expert. You will see the dialog as shown below:

Database Expert to select dataset  

+Select Project Data and select ADO.NET DataSets,  then you will see the TblProduct table. Select this table and click the arrow button to add the table to the Selected Table area. Click OK.

+Now back to the active window of the CrystalReport1 and drag fields of TblProduct table that you want to the designer area of the CrystalReport1.

Add table fields to the Crystal report designer  

-Step 4: Add a Crystal Report Viewer control to the form of the project( look for this control in the Toolbox) and writing C# code on the form load event.

+Accept the default name of the Crystal Report Viewer(crystalReportVewer1)

+Open the code window by double-click the form and in the Form1_load procedure write the following code:

CrystalReport1 rpt=new CrystalReport1(); //The report you created.
SqlConnection myCon;
SqlDataAdapter myAdapter;
DataSet1 myDataset=new DataSet1(); //The DataSet you created.
myCon = new SqlConnection("Server=(local);Database=SaleAndStock;User=sa; Password=123;");

myAdapter = new SqlDataAdapter("Select * FROM TblProduct", myCon);
myAdapter.Fill(myDataset, "TblProduct");
rpt.SetDataSource(myDataset);
crystalReportViewer1.ReportSource = rpt;

You also need to add the line of code below to the area above the namespace:

Using System.Data.SqlClient;

Therefore, the full C# code becomes as shown below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ReportCsharp
{
   public partial class Form1 : Form
   {
     public Form1()
     {
       InitializeComponent();
      }

    private void Form1_Load(object sender, EventArgs e)
    {
     CrystalReport1 rpt=new CrystalReport1(); //The report you created.
     SqlConnection myCon;
     SqlDataAdapter myAdapter;
     DataSet1 myDataset=new DataSet1(); //The DataSet you created.
     myCon = new SqlConnection("Server=(local)  ;Database=SaleAndStock;User=sa; Password=123;");

     myAdapter = new SqlDataAdapter("Select * FROM TblProduct", myCon);
     myAdapter.Fill(myDataset, "TblProduct");
     rpt.SetDataSource(myDataset);
     crystalReportViewer1.ReportSource = rpt;
     }
  }
}





Comments

Comments comment

 Comments

www.dotnetperls.com/position-windowsâ


12-16-2013
C# Crystal reports comment

 C# Crystal reports

More on crystal reports http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-tutorial.htm will cover most of the issues related to crystal reports

manly

09-10-2013
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like