Quantcast
Channel: Crystal Report Hosting News (SuperBlogAds Network) » crystal report hosting
Viewing all articles
Browse latest Browse all 17

Crystal Report Hosting :: Crystal Reports In Winforms Windows Forms With Parameters

$
0
0

Crystal Reports is a popular Windows-based report writer (report generation program) that allows a programmer to create reports from a variety of data sources with a minimum of written code. Developed by Seagate Software, Crystal Reports can access data from most widely-used databases and can integrate data from multiple databases within one report using Open Database Connectivity (ODBC).

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.

I have created two tables in database named Employees and Projects and fetching data from both tables and 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)

Follow steps below:

1. Create a new project in Visual Studio and go to solution explorer and add New Item > Crystal Report. Select Blank report option from the wizard window.

2. Click on CrystalReports menu > DataBase Expert.

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

4. Enter your SQL Server name , username and password , select database name from the dropdown and click OK.

5. In next window expand to find your tables and add them in right pane.

6. Click OK to finish.

7. 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.

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

9. 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).

10. 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

Viewing all articles
Browse latest Browse all 17

Latest Images

Trending Articles





Latest Images