Articles → ASP .NET GRIDVIEW → Send Gridview Data On Email In Asp.Net

Send Gridview Data On Email In Asp.Net






Software Requirement




  1. Visual studio 2005(or above) is installed on your machine.
  2. SQL Server 2005(or above) is installed on your machine

Prerequisite Knowledge




  1. Basics about SQL objects like tables.
  2. How to create tables in SQL server and how to insert data into the table?
  3. How to create a project using visual studio?
  4. Basic knowledge about basic controls like button textbox etc and their events
  5. Basic knowledge about data controls like grid view.
  6. How to bind data in grid view?
  7. How to send email using asp.net?

Steps Of Execution




  1. Create a new table
  2. Add data in the table
  3. Create a new project
  4. Add controls on the page
  5. Add code to bind grid
  6. Add code to send grid view on email



Create A New Table




Picture showing the schema of the sample database table
Click to Enlarge


Add Data In The Table




Picture showing the data in the sample database table
Click to Enlarge


Create A New Project




Picture showing the project structure in the solution explorer
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" />


  1. Gridview (gvReport) will display data fetch from database.
  2. Button (btnGetData) – Get data from database and bind gridview(gvReport)
  3. Button (btnSendOnEmail) – Email gridview’s data.

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




  1. Get the rendered HTML of the grid view
  2. Send rendered HTML on email as an email’s body.




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


Picture showing the output of sending gridview data on email in asp.net
Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250