How to Sendmail a selected row from datagridview that has been export to Crystal report

  • Thread starter Thread starter RinneMarpaung
  • Start date Start date
R

RinneMarpaung

Guest
Hai to all C# Master. Please help me, I am a newbie.
Before I start, really sorry for my bad english.



This is my issue :

I have some rows in my datagridview.

I already succeed export only selected row to crystal report.

I want to sendmail this selected row, but when I click btton sendmail, crystal report does not load my data, means that button send a blank report.


Here is my code :


private void button1_Click(object sender, EventArgs e)
{
// define a dataset
DataSet ds = new DataSet();

// define a datatable
DataTable dt = new DataTable();
dt.Columns.Add("EmployeeID", typeof(string));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("City", typeof(string));

// write datagridview data to datatable
foreach (DataGridViewRow dgr in dataGridView1.SelectedRows)
{
dt.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value, dgr.Cells[3].Value, dgr.Cells[4].Value, dgr.Cells[5].Value);
}

// add datatable to the dataset and convert to an XML format file
ds.Tables.Add(dt);
ds.WriteXmlSchema("Sample.xml");

// transfer data to crystalreportviewer
CrystalReport1 cr = new CrystalReport1();
cr.SetDataSource(ds);
crystalReportViewer.ReportSource = cr;
crystalReportViewer.Refresh();
}

private void button2_Click(object sender, EventArgs e) // button Send as Mail
{
ReportDocument cryRpt = crystalReportViewer.ReportSource as ReportDocument;
cryRpt.Load(@"C:\Users\Suriati\Documents\Visual Studio 2017\Projects\ExportToCrystalReport\ExportToCrystalReport\CrystalReport1.rpt");
crystalReportViewer.ReportSource = cryRpt;
crystalReportViewer.Refresh();



using (MailMessage mm = new MailMessage("suriatimarpaung85@gmail.com", "suriati@monstera.id"))
{
mm.Subject = "Employee Label";
mm.Body = "Employee Label";
mm.Attachments.Add(new Attachment(cryRpt.ExportToStream(ExportFormatType.PortableDocFormat), "EmployeeLabel.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
NetworkCredential credential = new NetworkCredential();
credential.UserName = "suriatimarpaung85@gmail.com";
credential.Password = "290831rsm";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credential;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(mm);
}
MessageBox.Show("Email Successfully Sent .");
}

Thanks in advance.

Continue reading...
 
Back
Top