//Here i have made a function which Loads an Already Designed Report
//in a Crystal Report Viewer and fills it with data
//this is same like you fill some DataGrid
// mysql is a Connection which has been already intialized so dont need to mention the Query String
using CrystalDecisions.CrystalReports; <OR in VB> imports CrystalDecisions.CrystalReports
//This is a simple SUB which takes an input parameter Query
// and use that Query string to get data.
public void LoadCarData(string strQuery)
{
try
{
//rptCars is a Crystal Report which was designed to View Cars Sales
rptCars rptc = new rptCars();
//Here Defining a SQL Data Adapter and giving some Qurey (select * from cars)
SqlDataAdapter myadp = new SqlDataAdapter(strQuery,mysql);
//Here Defining a new DataSet myds
DataSet myds = new DataSet();
//Now Filling the DataTable Cars in the Dataset myds
// Dont be confused with Cars table we give this name to the result
// Table for our Reference if you dont specify the name of Table
// by Default DataAdapter gives it name (Table1)
myadp.Fill(myds,"Cars");
//Next I am doing seting the CarReport Object to get data from this tables
rptc.SetDataSource(myds.Tables["Cars"]);
//once my report is filled with records i am setting Crystal Report Viewers
//Report Source Property to the Report Object which we instentiated from
//and already designed report.
reportviewer.ReportSource = rptc;
reportviewer.Zoom(2);
}
catch (Exception ex)
{
//If some error occures it will show it in a messageBox
MessageBox.Show(ex.Message,"Error");
}
finally
{
if (mysql.State==ConnectionState.Open)
{
mysql.Close();
}
}
}