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. SET QUOTED_IDENTIFIER ON
  6. GO
  7. SET ANSI_PADDING ON
  8. GO
  9. CREATE TABLE [dbo].[tblstudent](
  10. [id] [int] IDENTITY(1,1) NOT NULL,
  11. [Name] [varchar](100) NOT NULL,
  12. [MobileNo] [varchar](100) NOT NULL,
  13. [EmailId] [varchar](100) NOT NULL,
  14. [Address] [varchar](200) NOT NULL,
  15. CONSTRAINT [PK_tblstudent] PRIMARY KEY CLUSTERED
  16. (
  17. [id] ASC
  18. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  19. ) ON [PRIMARY]
  20. GO
  21. SET ANSI_PADDING OFF
  22. 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. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
  11. GridLines="None">
  12. <AlternatingRowStyle BackColor="White" />
  13. <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  14. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  15. <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
  16. <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
  17. <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
  18. <SortedAscendingCellStyle BackColor="#FDF5AC" />
  19. <SortedAscendingHeaderStyle BackColor="#4D0000" />
  20. <SortedDescendingCellStyle BackColor="#FCF6C0" />
  21. <SortedDescendingHeaderStyle BackColor="#820000" />
  22. </asp:GridView>
  23. </div>
  24. </form>
  25. </body>
  26. </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();
  14. }

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
  22. }

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();
  9. }

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 */
  4. }

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