In this article we
show you how to generate the dataGrid, datalist, gridview dynamically at
run time and then fill by dataset. That Grid content you send in email
or generate the excel file also. We can sent that excel file in Email
attachment. Similarly you can also create any other ASP.net control and
render it to get its complete HTML. This HTML you can send either in
email or save in database means you can perform any operation
string getData = "exec[ Stored Procedure name] "+userid+","+otherId+"";
SqlDataAdapter adpgetData = new SqlDataAdapter(getData, Config.DbConn);
DataSet dsGetData = new DataSet();
adpgetData.Fill(dsGetData);
if (dsGetData.Tables[0].Rows.Count > 0)
{
DataGrid dg = new DataGrid();
dg.DataSource = dsGetData.Tables[0];
dg.DataBind();
Hashtable htbl = new Hashtable();
string body = RenderGridView(dg);
htbl["@emailbody"] = body;
SendMail(ToEmailId, "Report:"+Name , "", "Report.htm", htbl, "", fromEmailId);
}
public static string RenderGridView(DataGrid dg)
{
string returnvalue = string.Empty;
try
{
StringWriter stringWrite = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringWrite);
VerifyRenderingInServerForm(dg);
dg.RenderControl(writer);
returnvalue = writer.InnerWriter.ToString();
}
catch (Exception ex)
{
}
return returnvalue;
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
string getData = "exec[ Stored Procedure name] "+userid+","+otherId+"";
SqlDataAdapter adpgetData = new SqlDataAdapter(getData, Config.DbConn);
DataSet dsGetData = new DataSet();
adpgetData.Fill(dsGetData);
if (dsGetData.Tables[0].Rows.Count > 0)
{
DataGrid dg = new DataGrid();
dg.DataSource = dsGetData.Tables[0];
dg.DataBind();
Hashtable htbl = new Hashtable();
string body = RenderGridView(dg);
htbl["@emailbody"] = body;
SendMail(ToEmailId, "Report:"+Name , "", "Report.htm", htbl, "", fromEmailId);
}
public static string RenderGridView(DataGrid dg)
{
string returnvalue = string.Empty;
try
{
StringWriter stringWrite = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringWrite);
VerifyRenderingInServerForm(dg);
dg.RenderControl(writer);
returnvalue = writer.InnerWriter.ToString();
}
catch (Exception ex)
{
}
return returnvalue;
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}