Tuesday, 6 November 2012

CrystalReportsInWindows


 Crystal Reports In Winforms Windows Forms With Parameters
Posted by amiT jaiN

In this example i am explaining how to create Crystal Reports In Winforms Or Windows Forms Application With Parameters from user to filter report using C#.NET and VB.NET

Crystal Reports In Winforms Or Windows Forms Application With Parameters
For this i have created two tables in database named Employees and Projects and fetching data from both tables

I've grouped results by Department name using group expert in crystal reports and put a dropdown on the form to select project name to display related report.


Employee table schema

ID    int
FirstName    varchar(50)
LastName    varchar(50)  
Department    varchar(50)  
ProjectID    numeric(18, 0)
Expenses    money  


Projects table schema

ProjectID    numeric(18, 0)  
ProjectName    varchar(50)





Create a new project in VS and go to solution explorer and add new item > crystal report.
Select Blank report option from the wizard window



Now click on CrystalReports menu and select DataBase Expert



Now in next window expand Create new connection section and OLEDB(ADO) and in next window select SQL Native Client









Enter you SQL Server name , username and password , select database name from the dropdown and click on ok



In next window expand to find your tables and add them in right pane
Click OK to finish



Now Right Click on Group Name Fields in Field Explorer and Select Group Expert.
In group expert box select the field on which you want data to be grouped.

 
Design your report by dragging the fields in section3 (Details)
my design look like this

In the form add a combobox and drag and drop CrystalReport Viewer from toobox. click on smart tag and choose the report we created earlier (CrystalReport1.rpt)
Form look like this


When we build and rum this report , it asks for Database login username and password , we need to provide database username and password in code behind.
 we need to write code in code behind to filter report based on user selected value or value provided by user
C# code behind
//Code to populate dropdown
//Fill dropdown in form_Load event by calling
//function written below
private void FillDropDown()
{
 SqlConnection con = new SqlConnection
  (ConfigurationManager.AppSettings["myConnection"]);
 SqlCommand cmd = new SqlCommand
("Select distinct ProjectID,ProjectName from Projects", con);
 con.Open();
 DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
dAdapter.Fill(objDs);
cmbMonth.DataSource = objDs.Tables[0];
cmbMonth.DisplayMember = "ProjectName";
cmbMonth.ValueMember = "ProjectID";
 cmbMonth.SelectedIndex = 0;
}
private void cmbMonth_SelectedIndexChanged
  (object sender, EventArgs e)
{
   //Create object of report
CrystalReport1 objReport = new CrystalReport1();

 //set database login information
objReport.SetDatabaseLogon
  ("amit", "password", @"AVDHESH\SQLEXPRESS", "TestDB");

//write formula to pass parameters to report
crystalReportViewer1.SelectionFormula
    ="{Projects.ProjectID} =" +cmbMonth.SelectedIndex;
crystalReportViewer1.ReportSource = objReport;
}


VB.NET code behind


Private Sub FillDropDown()
   Dim con As New SqlConnection
  (ConfigurationManager.AppSettings("myConnection"))

   Dim cmd As New SqlCommand
("Select distinct ProjectID,ProjectName from Projects", con)
  con.Open()
  Dim objDs As New DataSet()
  Dim dAdapter As New SqlDataAdapter()
  dAdapter.SelectCommand = cmd
   dAdapter.Fill(objDs)
   cmbMonth.DataSource = objDs.Tables(0)
   cmbMonth.DisplayMember = "ProjectName"
   cmbMonth.ValueMember = "ProjectID"
   cmbMonth.SelectedIndex = 0
    End Sub

Private Sub cmbMonth_SelectedIndexChanged
(ByVal sender As Object, ByVal e As EventArgs)

   'Create object of report
   Dim objReport As New CrystalReport1()

   'set database login information
   objReport.SetDatabaseLogon
("amit", "password", "AVDHESH\SQLEXPRESS", "TestDB")
   'write formula to pass parameters to report
  crystalReportViewer1.SelectionFormula= "{Projects.ProjectID} =" & cmbMonth.SelectedIndex


    crystalReportViewer1.ReportSource = objReport
End Sub



Hope this helps

No comments:

Post a Comment