<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" />
    &nbsp;
    <asp:Button ID="btnSendOnEmail" runat="server" Text="Send on email" OnClick="btnSendOnEmail_Click" />













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();        
}









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. */
    }















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;









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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250