>

Articles → ASP .NET GRIDVIEW → Send gridview data on email in asp.net

Send gridview data on email in asp.net




























Software requirement









Prerequisite knowledge



















Steps of execution



















Create a new table





Click to Enlarge


Add data in the table





Click to Enlarge


Create a new project





Click to Enlarge


Add controls on the page




<asp:GridView ID="gvReport" runat="server"  AutoGenerateColumns="false">
	<HeaderStyle Font-Names="Verdana" Font-Size="Small" BackColor="#90546f" ForeColor="White" BorderStyle="Solid" BorderColor="Black" />
	<RowStyle Font-Names="Verdana" Font-Size="Small"/>
	<Columns>
		<asp:TemplateField HeaderText="Course Name">
			<ItemTemplate>
				<asp:Label runat="server" Text='<%#Eval("course_name") %>'>
				</asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Course Code">
			<ItemTemplate>
				<asp:Label runat="server" Text='<%#Eval("course_code") %>'>
				</asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Due Date">
			<ItemTemplate>
				<asp:Label runat="server" Text='<%#Eval("due_date") %>'>
				</asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>
</asp:GridView>
<asp:Button ID="btnGetData" runat="server" Text="Get Data" OnClick="btnGetData_Click" />
     
    
<asp:Button ID="btnSendOnEmail" runat="server" Text="Send on email" OnClick="btnSendOnEmail_Click" />









Add code to bind grid




protected void btnGetData_Click(object sender, EventArgs e) {
  DataSet oDataSet = new DataSet();
  using(SqlConnection oConnection = new SqlConnection("connection_string")) {
    // Open a connection
    oConnection.Open();
    using(SqlDataAdapter adapter = new SqlDataAdapter("select * from Course", oConnection)) {

      adapter.Fill(oDataSet);
    }
  }

  gvReport.DataSource = oDataSet.Tables[0];
  gvReport.DataBind();
}





Add code to send grid view on email




protected void btnSendOnEmail_Click(object sender, EventArgs e) {
  StringBuilder htmlResponse = new StringBuilder();
  using(StringWriter sw = new StringWriter(htmlResponse)) {
    using(HtmlTextWriter textWriter = new HtmlTextWriter(sw)) {
      ((Control) gvReport).RenderControl(textWriter);
    }
  }

  MailMessage mailMessage = new MailMessage();
  mailMessage.From = new MailAddress("sender_address");
  mailMessage.To.Add("recipient_address");
  mailMessage.Subject = "Report";
  mailMessage.IsBodyHtml = true;
  mailMessage.Body = htmlResponse.ToString();

  SmtpClient smtpServer = new SmtpClient("smtp");
  smtpServer.Port = 25;
  smtpServer.Credentials = new System.Net.NetworkCredential("user_name", "password");
  smtpServer.EnableSsl = false;
  smtpServer.Send(mailMessage);
  mailMessage.Attachments.Clear();
  mailMessage = null;

}
public override void VerifyRenderingInServerForm(Control control) {
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
     server control at run time. */
}



Code explanation












StringBuilder htmlResponse = new StringBuilder();
using(StringWriter sw = new StringWriter(htmlResponse)) {
  using(HtmlTextWriter textWriter = new HtmlTextWriter(sw)) {
    ((Control) gvReport).RenderControl(textWriter);
  }
}






MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender_address");
mailMessage.To.Add("recipient_address");
mailMessage.Subject = "Report";
mailMessage.IsBodyHtml = true;
mailMessage.Body = htmlResponse.ToString();
        
SmtpClient smtpServer = new SmtpClient("smtp");
smtpServer.Port = 25;
smtpServer.Credentials = new System.Net.NetworkCredential("user_name", "password");
smtpServer.EnableSsl = false;
smtpServer.Send(mailMessage);
mailMessage.Attachments.Clear();
mailMessage = null;





Output



Click to Enlarge

Posted By  -  Karan Gupta
 
Posted On  -  Friday, April 12, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250