Adding parameters to the Crystal Report:

We can pass parameters to report during runtime. For that you need to follow the steps as below…
Main Report view -- > right click on the parameter fields items located in the field explorer
-- > select new -- > specify Customer_ID as the name -- > click ok -- > In the choose field dialog, choose CustomerId and click ok.

Now click the Select Expert button which is in the toolbar of Visual Studio 2005. In the Select Expert dialog, set the value of the control as CustomerId is equal to Customer_ID.

Now create a wizard in form1 using follows,
- Label (CustomerID )
- Combo Box (comboBox1)
- Button(ViewReports)

Write code in form1 code behind as follows to add parameters to report.

using System.Data;
using System.Data.SqlClient;


private void Form1_Load(object sender, EventArgs e)
{
string connStr = "Data Source=.\\SQLEXPRESS;Initial Catalog=myDB;Integrated Security=True";
string sql = "SELECT CustomerID FROM Customers";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader;
while (reader.Read)
{
comboBox1.Items.Add(reader(0));
}
conn.Close();
}


Now switch to button click event,
private void btnViewReport_Click(object sender, System.EventArgs e)
{
CrystalReport1 report = new CrystalReport1();
report.SetParameterValue("Customer_ID", ComboBox1.Text);
{
Form2.CrystalReportViewer1.ReportSource = report;
Form2.ShowDialog();
}
}


That’s it. Now you can test the application by running.

...S.VinothkumaR.

No comments: