attaching Grid View To mail

4 views
Skip to first unread message

P.V Raghavendra

unread,
Apr 18, 2014, 1:28:53 AM4/18/14
to raghu venkat, Raghu Venkat, Lavanya Chowdary, anuja ravipudi, datapoint_2013_dotnet
Design Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridMail.aspx.cs"
Inherits="GridMail" Debug="true" %>

<!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="GrdDemo" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"

style="z-index: 1; left: 178px; top: 126px; position: absolute;
height: 133px; width: 187px"
Visible="False">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="EmpCode" HeaderText="EmpCode"
SortExpression="EmpCode" />
</Columns>
</asp:GridView>
</div>
<asp:Button ID="BtnSend" runat="server" onclick="BtnSend_Click"
style="z-index: 1; left: 360px; top: 64px; position: absolute;
height: 26px"
Text="Send" />
<asp:TextBox ID="TxtEmail" runat="server"
style="z-index: 1; left: 200px; top: 76px; position:
absolute"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:RagConnectionString %>"
SelectCommand="SELECT [FirstName], [LastName], [EmpCode] FROM
[UserInfo]">
</asp:SqlDataSource>
<asp:Label ID="Label1" runat="server"
style="z-index: 1; left: 95px; top: 78px; position: absolute"
Text="Enter a Mail"></asp:Label>
</form>
</body>
</html>


Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.IO;
public partial class GridMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void BtnSend_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("mailtes...@gmail.com");
msg.To.Add(new MailAddress(TxtEmail.Text));
msg.Subject = "Employee Data";
msg.Body = "Please Check the Data";
msg.Body += GetGridviewData(GrdDemo);
msg.IsBodyHtml = true;
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("
mailtes...@gmail.com", "8008765262");
SmtpServer.EnableSsl = true;
SmtpServer.Send(msg);

}

private string GetGridviewData(GridView GrdDemo)
{
//throw new NotImplementedException();
StringBuilder str = new StringBuilder();
StringWriter Stw = new StringWriter(str);
HtmlTextWriter html = new HtmlTextWriter(Stw);
GrdDemo.RenderControl(html);
return str.ToString();

}
public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}
}


Design

[image: Inline image 1]


web.config File

<?xml version="1.0"?>

<!--
For more information on how to configure your ASP.NET application, please
visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>

<connectionStrings>
<add name="RagConnectionString" connectionString="Data
Source=.;Initial Catalog=Rag;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>

</configuration>

Table Design:
[image: Inline image 2]
GridMail.rar
Reply all
Reply to author
Forward
0 new messages