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

  • Question

  • 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.

    • Moved by CoolDadTx Tuesday, October 30, 2018 3:18 PM Winforms related
    • Moved by Stanly Fan Wednesday, October 31, 2018 1:24 AM
    Tuesday, October 30, 2018 8:06 AM

All replies

  • Hi,

    According to your description, your issue is more related about Crystal report, it will be more appropriate to ask your question at Crystal Reports Forum.

    Thank you for your understanding.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 31, 2018 1:24 AM