Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Send ASP.NET GridView Data as an Email

Article:
Viewed:  994 
Posted On:  02/12/2014 10:38:48 
How we can send ASP.NET Grid View Data as an Email in asp.net c# ? 

In this article I am going to show how we can send Grid View Data as an email in asp.net c#.

Below is Data Table in design mode from which I am showing data.

Send Grid View Data in Email

Image 1.

My Data Table script is:

CREATE TABLE [dbo].[Employee](
          [CompanyName] [varchar](50) NULL,
          [EmployeeCode] [int] NOT NULL,
          [EmployeeSupervisorCode] [int] NULL,
          [EmployeeName] [varchar](50) NULL,
          [ProjectName] [varchar](50) NULL,
          [JoiningDate] [datetime] NULL,
          [Experience] [varchar](50) NULL,
          [Mobile] [varchar](15) NULL,
          [Address] [varchar](50) NULL,
          [CreatedDate] [datetime] NULL,
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
          [EmployeeCode] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON
PRIMARY]
) ON [PRIMARY]
 
GO

Now My aspx is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Send Grid View Data as an Email</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="5" cellspacing="5" style="bordersolid 4px greenwidth70%"
            align="center">
            <tr>
                <td style="bordersolid 4px greenbackground-colorskybluefont-weightbold;
                    font-size22ptcolorBlue;" align="center">
                    Send Grid View Data as an Email
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridViewShowData" CellPadding="4" runat="server" Width="100%" ForeColor="#333333"
                        GridLines="None" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Left">
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                        <EditRowStyle BackColor="#999999" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td align="right" style="padding-right50px;">
                    <asp:Button ID="btnSendGridData" runat="server" Text="Send Grid View Data as an Email"
                        OnClick="btnSendGridData_Click" BorderStyle="Solid" BorderColor="Red" BackColor=SkyBlue />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 

Now my aspx.cs code is:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.IO;
 
public partial class _Default : System.Web.UI.Page
{
    SqlDataAdapter da;
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridviewData();
        }
    }


    
protected void BindGridviewData()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=MYPC\SqlServer2k8;Integrated Security=true;Initial Catalog=Test";
        SqlCommand cmd = new SqlCommand("SELECT CompanyName,EmployeeName,JoiningDate,Experience FROM EMPLOYEE", con);
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
 
        if (!object.Equals(ds.Tables[0], null))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridViewShowData.DataSource = ds;
                GridViewShowData.DataBind();
            }
            else
            {
                GridViewShowData.DataSource = null;
                GridViewShowData.DataBind();
            }
        }
    }
 
    protected void btnSendGridData_Click(object sender, EventArgs e)
    {
        SendGridDataAsAnEmail();
    }
 
    public void SendGridDataAsAnEmail()
    {
        MailMessage Msg = new MailMessage();
        MailAddress fromMail = new MailAddress("FromMail@gmail.com");
        Msg.From = fromMail;
        Msg.To.Add(new MailAddress("ToMail@gmail.com"));
        Msg.Subject = "Grid View All Data";
        Msg.Body = GridviewAllData(GridViewShowData);       
        Msg.IsBodyHtml = true;
        SmtpClient a = new SmtpClient();
        a.Host = "smtpout.secureserver.net";
        a.EnableSsl = false;
        NetworkCredential NetworkCred = new NetworkCredential();
        NetworkCred.UserName = Msg.From.Address;
        NetworkCred.Password = "xyz";
        a.UseDefaultCredentials = true;
        a.Credentials = NetworkCred;
        a.Port = 25;
        a.Send(Msg);
    }
    
    public string GridviewAllData(GridView gridV)
    {
        StringBuilder strBuilder = new StringBuilder();
        StringWriter strWriter = new StringWriter(strBuilder);
        HtmlTextWriter htw = new HtmlTextWriter(strWriter);
        gridV.RenderControl(htw);
        return strBuilder.ToString();
    } 

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

Now run the application and click on send Email button:

Send Grid View Data in Email

Image 2.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us