Monday, May 28, 2012

Create Report Using DataSet in C#

In the following example we will see how we can send data to report file using dataset.

SqlCommand dbcommand = new SqlCommand("sp_OperatorProcessShow", myConnection);
dbcommand.CommandType = CommandType.StoredProcedure;
dbcommand.Parameters.Add(new SqlParameter("@CompCode", CompanyCode));

// I have used Stored Procedure and supplied parameter

DataSetOP myDS = new DataSetOP(); // DataSetOP is the DataSet Name
DataTable dtMyTable = myDS.Tables["tblOperatorProcs"]; // tblOperatorProcs is the DataTable
dtMyTable.Clear();
DataRow myNewRow;
int count = 0;

SqlDataReader dbreader = dbcommand.ExecuteReader();

while (dbreader.Read())
{
      myNewRow = dtMyTable.NewRow();

      myNewRow[0] = dbreader[0];
      myNewRow[1] = dbreader[1];
      .....
      .....
      myNewRow[8] = dbreader[8]; // Add as many record as you need

      dtMyTable.Rows.Add(myNewRow);
      dtMyTable.AcceptChanges();
      count++;
}
dbreader.Close();

if (count > 0)
{
                CROprProcs myDataReport = new CROprProcs(); //
CROprProcs is a Crystal Report file
                myDataReport.SetDataSource(myDS.Tables["tblOperatorProcs"]);
                myDataReport.SetParameterValue("CompName", compName);
                crystalReportViewer1.ReportSource = myDataReport;
}
else
{
                MessageBox.Show("No Data Found");
}



No comments:

Post a Comment