Get the page Content , dataGrid, datalist, gridview content and send in Email

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 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();
                    if (dsGetData.Tables[0].Rows.Count > 0)
                        DataGrid dg = new DataGrid();
                        dg.DataSource = dsGetData.Tables[0];
                        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;
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter writer = new HtmlTextWriter(stringWrite);
            returnvalue = writer.InnerWriter.ToString();
        catch (Exception ex)
        return returnvalue;
    public override void VerifyRenderingInServerForm(Control control)

Related Alrticles

Add Your Business in Free Listing

FREE!!! Registration