Here I would like to share useful code to send gridview data
in an email message.
To accomplish our need, we need to write all the code in an
button event.when press send mail button event it will collect all the data
from the gridview and send the data through mail to another person…
Sendmail button code
protected void
Button2_Click(object sender, EventArgs e)
{
//***we need
to set as paging as false to mail all the data from the gridview.
GridView1.AllowPaging = false;
//***Again
bind the data into gridview
bindgrid();
//***Function
for to send mail
sendmail();
}
|
void
sendmail()
{
MailMessage mail = new MailMessage();
//**Enter to
mail address
mail.To.Add ("");
//** Enter
from adress
mail.From = new MailAddress("");
mail.IsBodyHtml = true;
mail.Subject = "";
//****Here we
call the method to get the data from the gridview
mail.Body = getgridiview(GridView1);
SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com",25);
smtp.Credentials = new
System.Net.NetworkCredential("username", "password");
try
{
smtp.Send(mail);
Response.Write("mail send");
}
catch (Exception
ex)
{
Response.Write(ex.ToString());
}
}
|
This is the code get the gridview data and retrun the data
as string
public string
getgridiview(GridView gv)
{
using (StringWriter sr = new StringWriter())
{
using (HtmlTextWriter ht=new HtmlTextWriter (sr))
{
Table tabe = new Table();
if
(gv.HeaderRow != null)
{
tabe.Rows.Add(gv.HeaderRow);
}
foreach (GridViewRow row in gv.Rows)
{
tabe.Rows.Add(row);
}
if
(gv.FooterRow != null)
{
tabe.Rows.Add(gv.FooterRow);
}
tabe.RenderControl(ht);
return
sr.ToString();
}
}
}
|
I hope you like this article …post your comments here..
Please click the download to get the whole project file
Post your comment …
0 comments:
Post a Comment