ColaReport - Demo 1
 Home
 FAQ
 Examples
 Documentation
 Download
 Feedback
 Support
  Demo 1       demo1.aspx.cs Complete Code       Return to Examples

This example demonstrates creating simple report listing.

Sample report output:

Demo: demo1.aspx

NamePhoneFax
 ABC Computers  508-555-1212  508-555-1213 
 Joe's Electronics  508-555-6543  508-555-6544 
 Bry's  650-555-9482  650-555-9483 
 Tech R Us  650-555-8392  650-555-8393 
 Schneider Computers  916-555-4938  650-555-4939 
 Mighty IT  714-555-8952  714-555-8960 

The code of Page_Load appears below:

	private void Page_Load(object sender, System.EventArgs e)
	{
		CellColumn oCol; 
		Report oReport;
		DataSet dsRpt;
		string sQuery;

		OleDbConnection oConn;
		OleDbDataAdapter daRpt;

		// Get connection string
		oConn = new OleDbConnection (ConfigurationSettings.AppSettings["gsConn"]);
		
		sQuery = "SELECT CustomerName, Phone1, Fax" +
			" FROM Customer";

		daRpt = new OleDbDataAdapter (sQuery, oConn);  
		dsRpt = new DataSet();
		daRpt.Fill(dsRpt);

		// Define Report 
		oReport = new Report ();
		oReport.LinesPerPage = 33;
		oReport.Style.Add ("font-family", "Verdana");
		oReport.HeaderRowAttributes.Attributes["bgcolor"] = "#efefef";

		oReport.FirstPageHeader.Text = "<h2>Demo: demo1.aspx</h2>";
		oReport.FirstPageHeader.Style.Add ("font-family", "Verdana");
		oReport.PageHeader.Text = "<h3>Demo: demo1.aspx</h3>";
		oReport.PageHeader.Style.Add ("font-family", "Verdana");

		oCol = oReport.CreateCellColumn ("CustomerName", "Name"); 
		oCol = oReport.CreateCellColumn ("Phone1", "Phone"); 
		oCol = oReport.CreateCellColumn ("Fax", "Fax"); 

		//Bind dataset
		oReport.Bind (dsRpt);

		//Set label text to report output
		lblReport.Text = oReport.Execute();
	}
      

First, all needed variables are declared:

	private void Page_Load(object sender, System.EventArgs e)
	{
		CellColumn oCol; 
		Report oReport;
		DataSet dsRpt;
		string sQuery;

		OleDbConnection oConn;
		OleDbDataAdapter daRpt;
      

Then, we initialize our connection, and execute some query to populate the DataSet containing the data to use. Note that we can also use a DataView instead of a DataSet.

		// Get connection string
		oConn = new OleDbConnection (ConfigurationSettings.AppSettings["gsConn"]);
		
		sQuery = "SELECT CustomerName, Phone1, Fax" +
			" FROM Customer";

		daRpt = new OleDbDataAdapter (sQuery, oConn);  
		dsRpt = new DataSet();
		daRpt.Fill(dsRpt);
      

Next, we instantiate a report. We set the number of lines per page, and set other formatting information.

		// Define Report 
		oReport = new Report ();
		oReport.LinesPerPage = 33;
		oReport.Style.Add ("font-family", "Verdana");
		oReport.HeaderRowAttributes.Attributes["bgcolor"] = "#efefef";

		oReport.FirstPageHeader.Text = "<h2>Demo: demo1.aspx</h2>";
		oReport.FirstPageHeader.Style.Add ("font-family", "Verdana");
		oReport.PageHeader.Text = "<h3>Demo: demo1.aspx</h3>";
		oReport.PageHeader.Style.Add ("font-family", "Verdana");
      

Now we define the columns that we want to appear in the report. The first argument to CreateCellColumn corresponds to the DataColumn in the DataSet. The second argument is for the column header that appears on the report.

		oCol = oReport.CreateCellColumn ("CustomerName", "Name"); 
		oCol = oReport.CreateCellColumn ("Phone1", "Phone"); 
		oCol = oReport.CreateCellColumn ("Fax", "Fax"); 
      

Finally, we bind the DataSet to the report, and set the output of the report to the report label.

	//Bind dataset
	oReport.Bind (dsRpt);

	//Set label text to report output
	lblReport.Text = oReport.Execute();
}
      
Copyright © 2003