How to Send Gridview Data in Email in ASP.Net using C#

Advertisement

In this Article I will explain how to send Gridview data to Email in Asp.net
I created a table in SQL as shown

USE [chinnu]
GO

/****** Object: Table [dbo].[EmployeeInfo] Script Date: 10/29/2013 02:35:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[EmployeeInfo](
[EmpId] [nvarchar](50) NOT NULL,
[EmpName] [nvarchar](50) NOT NULL,
[EmpAddress] [nvarchar](50) NOT NULL,
[EmpDesignation] [nvarchar](50) NOT NULL
) ON [PRIMARY]

GO

Now I am creating a Empty solution with Name GridViewMail and webform with name MailData.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MailData.aspx.cs" Inherits="GridViewMail.MailData" %>

<!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="gridview" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="EmpId" HeaderText="EmployeeId" InsertVisible="False"
/>
<asp:BoundField DataField="EmpName" HeaderText="EmployeeName" InsertVisible="False"
/>
<asp:BoundField DataField="EmpAddress" HeaderText="EmployeeAddress" />
<asp:BoundField DataField="EmpDesignation" HeaderText="EmployeeDesignation" />
</Columns>
</asp:GridView>
<asp:ImageButton runat = "server" ImageUrl ="~/email.gif" ToolTip = "Click Here TO Send Mail" Width = "70" Height = "55"
onclick="Emailbtn" />
</div>
</form>
</body>
</html>

Now I am writing the logic in MailData.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;
using System.IO;
using System.Text;

namespace GridViewMail
{
public partial class MailData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();

}
}
private void BindGridData()
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connstring"].ConnectionString))
{
SqlCommand command = new SqlCommand("SELECT * from EmployeeInfo", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
gridview.DataSource = dt;
gridview.DataBind();
}
}
catch (Exception ex)
{
}

}

public string gridviewData(GridView grid)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);

return sb.ToString();
}
protected void Emailbtn(object sender, ImageClickEventArgs e)
{
MailMessage mailmess = new MailMessage();
mailmess.From = new MailAddress("frommaild");
mailmess.To.Add(new MailAddress("tomailid"));
mailmess.Subject = "Send Employee Information";
mailmess.Body = "Please check the below Employee Information ";
mailmess.Body += gridviewData(gridview);
mailmess.IsBodyHtml = true;
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("yourmailid", "password");
sc.EnableSsl = true;
sc.Send(mailmess);



}

public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

}
}


After Completing Press F5.

OutPut:Click on Email ImageButton



Advertisements
SHARE

Lakshmi Tulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a comment