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.
- USE [csharp]
- GO
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[tblstudent](
- [id] [int] IDENTITY(1,1) NOT NULL,
- [Name] [varchar](100) NOT NULL,
- [MobileNo] [varchar](100) NOT NULL,
- [EmailId] [varchar](100) NOT NULL,
- [Address] [varchar](200) NOT NULL,
- CONSTRAINT [PK_tblstudent] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
-
- SET ANSI_PADDING OFF
- GO
Now I'm creating a project named “MailTheGrid”. Right-click in the project and add one WebForm and name it grid.aspx.
Grid.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="grid.aspx.cs" Inherits="MailTheGrid.grid" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
- GridLines="None">
- <AlternatingRowStyle BackColor="White" />
- <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
- <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
- <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
- <SortedAscendingCellStyle BackColor="#FDF5AC" />
- <SortedAscendingHeaderStyle BackColor="#4D0000" />
- <SortedDescendingCellStyle BackColor="#FCF6C0" />
- <SortedDescendingHeaderStyle BackColor="#820000" />
- </asp:GridView>
- </div>
- </form>
- </body>
- </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.
- public void BindDataToGrid()
- {
- SqlConnection conn = new SqlConnection(connec);
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandText = "select * from tblstudent";
- cmd.CommandType = CommandType.Text;
- DataSet ds = new DataSet();
- SqlDataAdapter da = new SqlDataAdapter();
- da.SelectCommand = cmd;
- da.Fill(ds);
- GridView1.DataSource = ds;
- 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.
- public void SendHTMLMail()
- {
- MailMessage Msg = new MailMessage();
- MailAddress fromMail = new MailAddress("[email protected]");
- Msg.From = fromMail;
- Msg.To.Add(new MailAddress("[email protected]"));
-
- Msg.Subject = "Mail the Grid " + DateTime.Now.AddDays(-1).ToString("dd/MMM/yyyy");
- Msg.Body = GetGridviewData(GridView1);
- Msg.Body += "<br/> Thank & Regards,<br/>Venkat Kumar Chigulla ";
- Msg.IsBodyHtml = true;
- SmtpClient a = new SmtpClient();
- a.Host = "smtpout.secureserver.net";
- a.EnableSsl = false;
- NetworkCredential NetworkCred = new NetworkCredential();
- NetworkCred.UserName = Msg.From.Address;
- NetworkCred.Password = "abc";
- a.UseDefaultCredentials = true;
- a.Credentials = NetworkCred;
- a.Port = 3535;
- a.Send(Msg);
- }
If you observe closely, the preceding function is calling GetGridViewData().
-
- public string GetGridviewData(GridView gv)
- {
- StringBuilder strBuilder = new StringBuilder();
- StringWriter strWriter = new StringWriter(strBuilder);
- HtmlTextWriter htw = new HtmlTextWriter(strWriter);
- gv.RenderControl(htw);
- return strBuilder.ToString();
- }
Here we have one override method that will verify whether or not the control is rendered.
- public override void VerifyRenderingInServerForm(Control control)
- {
-
- }
Now call the BindDataToGrid() and SendHTMLMail() functions in the page load event.
- string connec = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Page.IsPostBack == false)
- {
- BindDataToGrid();
- SendHTMLMail();
- }
- }