Articles → ASP .NET GRIDVIEW → Send Gridview Data On Email In Asp.Net
Send Gridview Data On Email In Asp.Net
Software Requirement
- Visual studio 2005(or above) is installed on your machine.
- SQL Server 2005(or above) is installed on your machine
Prerequisite Knowledge
- Basics about SQL objects like tables.
- How to create tables in SQL server and how to insert data into the table?
- How to create a project using visual studio?
- Basic knowledge about basic controls like button textbox etc and their events
- Basic knowledge about data controls like grid view.
- How to bind data in grid view?
- How to send email using asp.net?
Steps Of Execution
- Create a new table
- Add data in the table
- Create a new project
- Add controls on the page
- Add code to bind grid
- Add code to send grid view on email
Create A New Table
Click to Enlarge
Add Data In The Table
Click to Enlarge
Create A New Project
Click to Enlarge
Add Controls On The Page
<asp:GridView ID="gvReport" runat="server" AutoGenerateColumns="false">
<HeaderStyle Font-Names="Verdana" Font-Size="Small" BackColor="#90546f" ForeColor="White" BorderStyle="Solid" BorderColor="Black" />
<RowStyle Font-Names="Verdana" Font-Size="Small"/>
<Columns>
<asp:TemplateField HeaderText="Course Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("course_name") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Code">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("course_code") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Due Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("due_date") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnGetData" runat="server" Text="Get Data" OnClick="btnGetData_Click" />
<asp:Button ID="btnSendOnEmail" runat="server" Text="Send on email" OnClick="btnSendOnEmail_Click" />
- Gridview (gvReport) will display data fetch from database.
- Button (btnGetData) – Get data from database and bind gridview(gvReport)
- Button (btnSendOnEmail) – Email gridview’s data.
Add Code To Bind Grid
protected void btnGetData_Click(object sender, EventArgs e) {
DataSet oDataSet = new DataSet();
using(SqlConnection oConnection = new SqlConnection("connection_string")) {
// Open a connection
oConnection.Open();
using(SqlDataAdapter adapter = new SqlDataAdapter("select * from Course", oConnection)) {
adapter.Fill(oDataSet);
}
}
gvReport.DataSource = oDataSet.Tables[0];
gvReport.DataBind();
}
Add Code To Send Grid View On Email
protected void btnSendOnEmail_Click(object sender, EventArgs e) {
StringBuilder htmlResponse = new StringBuilder();
using(StringWriter sw = new StringWriter(htmlResponse)) {
using(HtmlTextWriter textWriter = new HtmlTextWriter(sw)) {
((Control) gvReport).RenderControl(textWriter);
}
}
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender_address");
mailMessage.To.Add("recipient_address");
mailMessage.Subject = "Report";
mailMessage.IsBodyHtml = true;
mailMessage.Body = htmlResponse.ToString();
SmtpClient smtpServer = new SmtpClient("smtp");
smtpServer.Port = 25;
smtpServer.Credentials = new System.Net.NetworkCredential("user_name", "password");
smtpServer.EnableSsl = false;
smtpServer.Send(mailMessage);
mailMessage.Attachments.Clear();
mailMessage = null;
}
public override void VerifyRenderingInServerForm(Control control) {
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
Code Explanation
- Get the rendered HTML of the grid view
- Send rendered HTML on email as an email’s body.
StringBuilder htmlResponse = new StringBuilder();
using(StringWriter sw = new StringWriter(htmlResponse)) {
using(HtmlTextWriter textWriter = new HtmlTextWriter(sw)) {
((Control) gvReport).RenderControl(textWriter);
}
}
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender_address");
mailMessage.To.Add("recipient_address");
mailMessage.Subject = "Report";
mailMessage.IsBodyHtml = true;
mailMessage.Body = htmlResponse.ToString();
SmtpClient smtpServer = new SmtpClient("smtp");
smtpServer.Port = 25;
smtpServer.Credentials = new System.Net.NetworkCredential("user_name", "password");
smtpServer.EnableSsl = false;
smtpServer.Send(mailMessage);
mailMessage.Attachments.Clear();
mailMessage = null;
Output
Click to Enlarge