troubleshooting Question

Sending only one email for a gridview row when muliplle rows are selected via a checkbox

Avatar of newjeep19
newjeep19Flag for United States of America asked on
.NET ProgrammingC#Email Servers
19 Comments1 Solution494 ViewsLast Modified:
I have a grid that when a checkbox or checkboxes in the rows in the grid are checked then an email is generated. However, I am getting an email sent when every row is checked. So, if there is a grid that has six rows and three of the rows checkboxes are checked then i get three emails instead of one email that shows all three checked rows.
The code is below:
C# Code:
 // ACTION email :  Email to Service Support Team using Windows Authentication
                try
                {

                    // Html output buffer.
                    StringBuilder sbHtml = new StringBuilder();

                    using (StringWriter sw = new StringWriter(sbHtml))
                    {
                        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                        {
                            // Set the custom render method for all the controls inside GridView (gvParent).
                            SetMyCustomRenderMethodDelegate(hw, gvParent);

                            // Render GridView (gvParent) into a string builder.
                            gvParent.RenderControl(hw);
                        }
                    }

                    // Declare a string variable to hold senders email
                    string senderAddress = Request.ServerVariables["LOGON_USER"].ToString().Split('\\')[1];

                    // If sender is null, then user not found or has no email address
                    if (senderAddress == null)
                    {
                        lblMessage.Text = "Your user account was not found, or you have no email address - email message not sent.";
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                        return;
                    }

                   foreach (GridViewRow gr in gvParent.Rows)
                   {
                        Label txtVendor = (Label)gr.FindControl("txtVendor");
                        Label txtGTRIPONumb = (Label)gr.FindControl("txtGTRIPONumb");
                        CheckBox cb = (CheckBox)gr.FindControl("chkRequestSNT");
                        HiddenField hfCustomer = (HiddenField)gr.FindControl("AccountIdNameID"); 
                        if (cb.Checked)
                        {
                            MailMessage mailIAM = new MailMessage();

                            mailIAM.From = new MailAddress(senderAddress + "@company.com");
                            mailIAM.To.Add("servicesupport@company.com");
                            mailIAM.Subject = "Request SNT for: " + "Customers(s) : " + hfCustomer.Value.ToString();  //"Vendor(s) : " + txtVendor.Text + " " + "|" + " " + "GTRI PO # : " + txtGTRIPONumb.Text + " " + "|"  " + 
                            mailIAM.IsBodyHtml = true;
                            mailIAM.BodyEncoding = System.Text.Encoding.UTF32; 
                            string url = string.Format("http://crm/{0}/sfa/salesorder/edit.aspx?id={1}",crmOrganization,_orderId);
                            mailIAM.Body = sbHtml.ToString() + @"<br/>Link to Order : <a href='" + url + "'>CRM Order</a>"; 
                                                                                                                
                            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                            smtp.Host = "company.com";
                            smtp.Port = 25;
                            smtp.Send(mailIAM);
                        }
                        else
                        {
                            CheckBox chk = (CheckBox)gr.FindControl("chkSNTOrder");
                            if (chk.Checked)
                            {
                                MailMessage mailIAM = new MailMessage();

                                mailIAM.From = new MailAddress(senderAddress + "@company.com");
                                mailIAM.To.Add("servicesupport@company.com");
                                mailIAM.Subject = "SNT Order for: " + "Customers(s) : " + hfCustomer.Value.ToString(); 
                                mailIAM.IsBodyHtml = true;
                                mailIAM.BodyEncoding = System.Text.Encoding.UTF32;
                                string url = string.Format("http://crm/{0}/sfa/salesorder/edit.aspx?id={1}", crmOrganization, _orderId);
                                mailIAM.Body = sbHtml.ToString() + @"<br/>Link to Order : <a href='" + url + "'>CRM Order</a>"; 
                                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                                smtp.Host = "company.com";
                                smtp.Port = 25;
                                smtp.Send(mailIAM);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblMessage.Text = "An error occurred sending your e-mail, the error is:\r\n" + ex.Message;
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    return;
                }

  // Helper method.
        void SetMyCustomRenderMethodDelegate(HtmlTextWriter writer, Control control)
        {
            if (control is GridViewRow)
                control.SetRenderMethodDelegate(MyCustomRender);

            foreach (Control childControl in control.Controls)
            {
                SetMyCustomRenderMethodDelegate(writer, childControl);
            }
        }

        // ********************* DO NOT DELETE ********************** \\
        // Required to avoid the "GridView must be placed inside a form tag with runat=server" error.
        public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }

        #region Misc Function
        //FUNCTION : Checks if the checkbox is checked in the grideview row and retrieve all the selected items in the GridView.
        // Excludes hidden rows, checkboxes and buttons in the gridview.
        void MyCustomRender(HtmlTextWriter writer, Control control)
        {
            // Remove the delegate to avoid infinite loop.
            control.SetRenderMethodDelegate(null);

            if (control is TableRow)
            {
                TableRow row = (TableRow)control;

                // Logic to render only the checked rows.
                CheckBox cb = control.FindControl("chkRequestSNT") as CheckBox;
                CheckBox chk = control.FindControl("chkSNTOrder") as CheckBox;
                int index = 0;
                foreach (TableCell item in row.Cells)
                {
                    index++;
                    switch (index)
                    {

                        case 1:
                        case 13:
                            //Ignore column 1 and 13
                            continue;
                        default:
                            if (cb == null || chk == null || cb.Checked || chk.Checked)

                                item.RenderControl(writer);
                            break;
                    }
                }
            }
            else
            {
                control.RenderControl(writer);
            }
        }

Please help
ASKER CERTIFIED SOLUTION
newjeep19

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 19 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 19 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros