How to Mail the Grid in ASP.Net by Using C#

Today I'm explaining how to mail a Grid in ASP.Net using C#.

First of all I'm creating one table (tblstudent) and inserting 3 records.

  1. USE [csharp]  
  2. GO  
  3. SET ANSI_NULLS ON  
  4. GO  
  5.   
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8.   
  9. SET ANSI_PADDING ON  
  10. GO  
  11.   
  12. CREATE TABLE [dbo].[tblstudent](  
  13.     [id] [int] IDENTITY(1,1) NOT NULL,  
  14.     [Name] [varchar](100) NOT NULL,  
  15.     [MobileNo] [varchar](100) NOT NULL,  
  16.     [EmailId] [varchar](100) NOT NULL,  
  17.     [Address] [varchar](200) NOT NULL,  
  18.  CONSTRAINT [PK_tblstudent] PRIMARY KEY CLUSTERED   
  19. (  
  20.     [id] ASC  
  21. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]  
  22. ON [PRIMARY]  
  23.   
  24. GO  
  25.   
  26. SET ANSI_PADDING OFF  
  27. GO 

query results

Now I'm creating a project named “MailTheGrid”. Right-click in the project and add one WebForm and name it grid.aspx.

Grid.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="grid.aspx.cs" Inherits="MailTheGrid.grid" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"   
  13.             GridLines="None">  
  14.             <AlternatingRowStyle BackColor="White" />  
  15.             <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  16.             <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  17.             <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center"             />  
  18.             <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />  
  19.             <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
  20.             <SortedAscendingCellStyle BackColor="#FDF5AC" />  
  21.             <SortedAscendingHeaderStyle BackColor="#4D0000" />  
  22.             <SortedDescendingCellStyle BackColor="#FCF6C0" />  
  23.             <SortedDescendingHeaderStyle BackColor="#820000" />  
  24.         </asp:GridView>  
  25.     </div>  
  26.     </form>  
  27. </body>  
  28. </html> 

Grid.aspx.cs

Before mailing the grid we need to put data in the Grid View. For that I'm using the following function.

  1. public void BindDataToGrid()  
  2. {  
  3.     SqlConnection conn = new SqlConnection(connec);  
  4.     SqlCommand cmd = new SqlCommand();  
  5.     cmd.Connection = conn;  
  6.     cmd.CommandText = "select * from tblstudent";  
  7.     cmd.CommandType = CommandType.Text;  
  8.     DataSet ds = new DataSet();  
  9.     SqlDataAdapter da = new SqlDataAdapter();  
  10.     da.SelectCommand = cmd;  
  11.     da.Fill(ds);  
  12.     GridView1.DataSource = ds;  
  13.     GridView1.DataBind();  

Now the Grid View has some data. Now we will see how to mail this data.

Here I'm writing a function to mail.

  1. public void SendHTMLMail()  
  2. {  
  3.      MailMessage Msg = new MailMessage();  
  4.      MailAddress fromMail = new MailAddress("[email protected]");  
  5.      Msg.From = fromMail;  
  6.      Msg.To.Add(new MailAddress("[email protected]"));  
  7.      // Subject of e-mail  
  8.      Msg.Subject = "Mail the Grid " + DateTime.Now.AddDays(-1).ToString("dd/MMM/yyyy");  
  9.      Msg.Body = GetGridviewData(GridView1);  
  10.      Msg.Body += "<br/> Thank & Regards,<br/>Venkat Kumar Chigulla ";  
  11.      Msg.IsBodyHtml = true;  
  12.      SmtpClient a = new SmtpClient();  
  13.      a.Host = "smtpout.secureserver.net";  
  14.      a.EnableSsl = false;  
  15.      NetworkCredential NetworkCred = new NetworkCredential();  
  16.      NetworkCred.UserName = Msg.From.Address;  
  17.      NetworkCred.Password = "abc";  
  18.      a.UseDefaultCredentials = true;  
  19.      a.Credentials = NetworkCred;  
  20.      a.Port = 3535;  
  21.      a.Send(Msg); //sending Email  

If you observe closely, the preceding function is calling GetGridViewData().

  1. // This Method is used to render gridview control  
  2. public string GetGridviewData(GridView gv)  
  3. {  
  4.      StringBuilder strBuilder = new StringBuilder();  
  5.      StringWriter strWriter = new StringWriter(strBuilder);  
  6.      HtmlTextWriter htw = new HtmlTextWriter(strWriter);  
  7.      gv.RenderControl(htw);  
  8.      return strBuilder.ToString();  

Here we have one override method that will verify whether or not the control is rendered.

  1. public override void VerifyRenderingInServerForm(Control control)  
  2. {  
  3.      /* Verifies that the control is rendered */  

Now call the BindDataToGrid() and SendHTMLMail() functions in the page load event.

  1. string connec = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;  
  2. protected void Page_Load(object sender, EventArgs e)  
  3. {  
  4.      if (Page.IsPostBack == false)  
  5.      {  
  6.          BindDataToGrid();  
  7.          SendHTMLMail();  
  8.      }  
  9. }  

output


Similar Articles