Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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
Introduction:

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

Description:

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

To implement this concept design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>send gridview in email body in asp.net using C#,VB.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
<asp:Button ID="btnSendMail" runat="server" Text="Send Gridview As Mail" onclick="btnSendMail_Click" />
</form>
</body>
</html>
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)
{
BindGridview();
}
}

// 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");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 10 UserName,FirstName,LastName,Location FROM UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
protected void btnSendMail_Click(object sender, EventArgs e)
{
SendHTMLMail();
}

// 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("administrator@aspdotnet-suresh.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress("suresh@gmail.com"));
// 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 = "10.2.160.101";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.EnableSsl = true;
a.Send(Msg);
}
// 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);
gv.RenderControl(htw);
return strBuilder.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

VB.NET Code


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
BindGridview()
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")
con.Open()
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()
da.Fill(ds)
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
End Sub
Protected Sub btnSendMail_Click(sender As Object, e As EventArgs)
SendHTMLMail()
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("administrator@aspdotnet-suresh.com")
' Sender e-mail address.
Msg.From = fromMail
' Recipient e-mail address.
Msg.[To].Add(New MailAddress("suresh@gmail.com"))
' 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 = "10.2.160.101"
Dim a As New SmtpClient()
a.Host = sSmtpServer
a.EnableSsl = True
a.Send(Msg)
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)
gv.RenderControl(htw)
Return strBuilder.ToString()
End Function
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
End Class
Demo

Download sample code attached

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

Get the latest Asp.net, C#.net, 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

16 comments :

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 asp.net ?

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 asp.net 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 window.click 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 = " + "\"window.open(" + "\'http://www.google.com\'" + ");\"" ;
I am using C#.
please help
Thanks

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

Give your Valuable Comments

Other Related Posts

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