Monday, June 20, 2011

How to generate dynamic report.

Below is the code that I had written for generating dynamic report. It takes few parameters and generates the reports for a particular frequency.



using System;

using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace Reports.DynamicReports
{
///
/// Summary description for GetDynamicFrequencyReport.
///

public class GetDynamicFrequencyReport : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder plResults;


ReportDS rds;
DataSet ds;


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

// Put user code to initialize the page here

int intid;
string strSourceid ;
int intCount=0;
DateTime dtFromDate;
DateTime dtToDate;
string strFrequency;

string strSourceFromDate;
string strSourceToDate;
string strSourceFrequency;

int inti;

if (Request["id"]!=null)
{
strSourceid = Request["id"];
strSourceFromDate = Request["fromdate"];
strSourceToDate=Request["todate"];
strSourceFrequency = Request["frequency"];

try
{
intid = int.Parse(strSourceid);
dtFromDate = DateTime.Parse(strSourceFromDate);
dtToDate = DateTime.Parse(strSourceToDate);
strFrequency = strSourceFrequency;

rds = new ReportDS();
ds = rds.GetDynamiFrequencyReport(intid,dtFromDate,dtToDate,strFrequency);

if (ds != null && ds.Tables.Count > 0 )
{
intCount = int.Parse(ds.Tables[0].Rows[0][0].ToString());


for (inti = 1; inti <= intCount ; inti++) { DynamicTables(inti); } } } catch(Exception ex) {} } } private void DynamicTables(int inti) { int intLabelTable; int intGridTable; intLabelTable = (inti * 2 )-1; intGridTable = (inti * 2 ); AddToPlaceHolder(CreateLabel(ds.Tables[intLabelTable].Rows[0][0].ToString())); AddToPlaceHolder(CreateDataGrid(ds.Tables[intGridTable])); } private void AddToPlaceHolder(Control oControl) { try { if (oControl != null) { plResults.Controls.Add(oControl); } } catch(Exception Ex) { } } private Label CreateLabel(string sText) { Label oControl = new Label(); oControl.Text = sText; oControl.Font.Name = "Verdana"; oControl.Font.Size = 11; oControl.Font.Italic = true; oControl.Font.Bold = true; return oControl; } private DataGrid CreateDataGrid(DataTable dt) { DataGrid oControl = new DataGrid(); // //
//
//

//
//
//



oControl.BorderColor= Color.Black;
oControl.BorderStyle = BorderStyle.None;
oControl.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
oControl.BackColor = System.Drawing.Color.FromArgb(247,247,247);
oControl.CellPadding = 4;
oControl.Font.Name = "Verdana";
oControl.Font.Size = 9;


//

oControl.AlternatingItemStyle.BackColor = System.Drawing.Color.FromArgb(235,235,235);

//


oControl.PagerStyle.ForeColor = System.Drawing.Color.FromArgb(51,00,153);
oControl.PagerStyle.BackColor = System.Drawing.Color.FromArgb(255,255,204);



oControl.GridLines= GridLines.Vertical;
oControl.CellPadding = 3;



oControl.FooterStyle.ForeColor=Color.Black;
oControl.FooterStyle.BackColor=Color.LightGray;

oControl.AlternatingItemStyle.BackColor =Color.Gainsboro;
oControl.ItemStyle.ForeColor = Color.Black;

oControl.HeaderStyle.Font.Bold = true;
oControl.HeaderStyle.ForeColor=Color.White;
oControl.HeaderStyle.BackColor=Color.Blue;

oControl.DataSource = dt;


oControl.DataBind();

return oControl;
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}