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

Posted by Rajesh081725 under VB.NET category on | Points: 40 | Views : 2472
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" />

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

Comments or Responses

Login to post response