aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Send Gridview in Email Body in Asp.Net Using C#, VB.NET

Sep 18, 2012

In this article I will explain how to send Gridview as email body in using C# and VB.NET.


In previous article I explained
Send HTML file as Email body in, Send Mail in, Send mail with images using gmail credentials, Send mail with attachment in and many articles relating to JQuery,, SQL Server etc. Now I will explain how to send Gridview as email body in using C# and VB.NET.

To implement this concept design your aspx page like this

<html xmlns="">
<head id="Head1">
<title>send gridview in email body in using C#,VB.NET</title>
<form id="form1" runat="server">
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<asp:Button ID="btnSendMail" runat="server" Text="Send Gridview As Mail" onclick="btnSendMail_Click" />
Now add the following namespaces in code behind

C# Code

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
After add namespaces write the following code in code behind

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)

// This method is used to bind gridview from database
protected void BindGridview()
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
SqlCommand cmd = new SqlCommand("SELECT TOP 10 UserName,FirstName,LastName,Location FROM UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
gvUserInfo.DataSource = ds;
protected void btnSendMail_Click(object sender, EventArgs e)

// Method Which is used to Get HTML File and replace HTML File values with dynamic values and send mail
public void SendHTMLMail()
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress(""));
// Subject of e-mail
Msg.Subject = "Send Gridivew in EMail";
Msg.Body += "Please check below data <br/><br/>";
Msg.Body += GetGridviewData(gvUserInfo);
Msg.IsBodyHtml = true;
string sSmtpServer = "";
sSmtpServer = "";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.EnableSsl = true;
// This Method is used to render gridview control
public string GetGridviewData(GridView gv)
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
return strBuilder.ToString();
public override void VerifyRenderingInServerForm(Control control)
/* Verifies that the control is rendered */


Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Imports System.Net.Mail
Imports System.Text
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Class VBCodeSendGridviewMail
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
End If
End Sub

' This method is used to bind gridview from database

Protected Sub BindGridview()
Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Dim cmd As New SqlCommand("SELECT TOP 10 UserName,FirstName,LastName,Location FROM UserInformation", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
gvUserInfo.DataSource = ds
End Sub
Protected Sub btnSendMail_Click(sender As Object, e As EventArgs)
End Sub

' Method Which is used to Get HTML File and replace HTML File values with dynamic values and send mail
Public Sub SendHTMLMail()
Dim Msg As New MailMessage()
Dim fromMail As New MailAddress("")
' Sender e-mail address.
Msg.From = fromMail
' Recipient e-mail address.
Msg.[To].Add(New MailAddress(""))
' Subject of e-mail
Msg.Subject = "Send Gridivew in EMail"
Msg.Body += "Please check below data <br/><br/>"
Msg.Body += GetGridviewData(gvUserInfo)
Msg.IsBodyHtml = True
Dim sSmtpServer As String = ""
sSmtpServer = ""
Dim a As New SmtpClient()
a.Host = sSmtpServer
a.EnableSsl = True
End Sub
' This Method is used to render gridview control
Public Function GetGridviewData(gv As GridView) As String
Dim strBuilder As New StringBuilder()
Dim strWriter As New StringWriter(strBuilder)
Dim htw As New HtmlTextWriter(strWriter)
Return strBuilder.ToString()
End Function
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
End Class

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Eaton Hudson said...

I have gone through many of your tutorials to ASP.NET and would say that they are amazing to learn something fruitful out of it. I recommend beginners to follow your tutorials.

darpan pathak said...

sir can we do socket programming in ?

Anonymous said...

vn mnnnmn ghjgh ghjj

Anonymous said...

can you explain?
what these classes do actually & differences strBuilder , strWriter and HtmlTextWriter?

RAJ said...

sir i want to send listview data to email with image how to do that.

Srikanth said...

Please Upload the Sending Sms Code

srikanth said...

sir i am trying to build an web page with gridview and some other details in a web page.
the theme is in a gridview we have a checkbox when we submit a button the selected checkbox should be sent a mail with the details to the other person

srikanth said...

and i also need a code for getting data from database using multiple dropdownlists

Anonymous said...

how to open attachment in popup window from gridview in with print button????

Anonymous said...

I am sending a html input button in msg body through email, On clicking of this button from mail I want to open a web page... I have added in onclick event but nothing is happening when i click on button, do i need to set any property in order to makr the button work from email. here is the code for msg.body :
mail.Body = "input id="b1" type="button" onclick = " + "\"" + "\'\'" + ");\"" ;
I am using C#.
please help

Anonymous said...

thanks for sharing,

can you forward this by daily auto scheduling.

Anonymous said...

where is the authentication for the smtp server?

Anonymous said...

Hello sir.... I'm kohila...
I have used your many codes in my project.
These code also work finely.
But, i want to use these concept in a c# page which including master page.
i have tried these,
But while including master page,
i have an error that,
"Control 'ctl00_Contentplaceholder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
Please check it....
And give idea to use these code even the page have include masterpage..

Anonymous said...

Hi Anonymous (13),

You must included this code:
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub

And add this on your aspx page:
<%@ EnableEventValidation="false" %>

Anonymous said...

how to download that gridview from inbox

Anonymous said...

i am facing an error here as Msg.Body += GetGridviewData(gvUserInfo) has some invalid arguments

Hardik Radia said...

sir how to code for send email of images in grid view.

Hardik Radia said...

Sir how to code to send email of gridview containing one image column in sir please reply as fast as possible its urgent.thanks

Give your Valuable Comments

Other Related Posts

© 2010-2012 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.