0

I am trying to control whether or not an email address is already in a list, and if so i'd like to add a row from a gridview to the new gridview that will be send to that email account.

I have some code to get the rows and send them to the email address. But the problem is that it only sends 1 row to an email address even though multiple rows for that address were checked.

See here my code:

protected void ButtonATH_Click(object sender, EventArgs e)
    {
        List<string> lst = new List<string>();
        for (int i = 0; i < GridViewBestelling.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridViewBestelling.Rows[i].Cells[0].FindControl("CheckBoxAfTeHalen");
            Label bestelID = (Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID");
            Label lblUsrE = (Label)GridViewBestelling.Rows[i].Cells[7].FindControl("LabelEmailGeb");
            string emadr = lblUsrE.Text.ToString();
            string conn = "Data Source=pc-...";

            GridView grd = new GridView();

         if (ck.Checked)
            {
                System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
                sqlConn.Open();
                System.Data.SqlClient.SqlCommand updateCommand = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET tBestAfTeHalen = '" + ck.Checked + "' WHERE tBestId= '" + bestelID.Text + "'", sqlConn);
                updateCommand.Parameters.AddWithValue("@bestID", bestelID.Text);
                updateCommand.ExecuteNonQuery();

                LabelSendGridBoven.Text = "<b>Hello: <br /><br /> ";
                LabelSendGridBoven.Visible = false;

                LabelHoeveelheid.Text = "<br /><br /> Amount" ;
                LabelHoeveelheid.Visible = false;

                LabelSendGridTussen.Text = "Regards";
                LabelSendGridTussen.Visible = false;

                LabelSendGridOnder.Text = "<br /><br />----------------------" ;
                LabelSendGridOnder.Visible = false;

                DataTable dt = new DataTable();
                DataRow dr;

                dt.Columns.Add(new DataColumn("B_ID"));
                dt.Columns.Add(new DataColumn("P"));
                dt.Columns.Add(new DataColumn("M"));
                dt.Columns.Add(new DataColumn("H"));

                dr = dt.NewRow();
                dr["B_ID"] = ((Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID")).Text;
                dr["P"] = ((Label)GridViewBestelling.Rows[i].Cells[8].FindControl("LabelProduct_naam")).Text;
                dr["M"] = ((Label)GridViewBestelling.Rows[i].Cells[9].FindControl("LabelManufact_Nr")).Text;
                dr["H"] = ((Label)GridViewBestelling.Rows[i].Cells[3].FindControl("LabelHoeveelheid")).Text;
                dt.Rows.Add(dr);

                grd.DataSource = dt;
                grd.DataBind();

                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                grd.RenderControl(htw);

              if ( !lst.Contains(emadr))
              {
                  lst.Add(emadr);

                  System.Data.SqlClient.SqlConnection sqlConn2 = new System.Data.SqlClient.SqlConnection(conn);
                  sqlConn2.Open();
                  System.Data.SqlClient.SqlCommand updateCommand2 = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET Status=@statusChange WHERE tBestId= '" + bestelID.Text + "'", sqlConn2);
                  updateCommand2.Parameters.AddWithValue("@statusChange", statusChange.ToString());
                  updateCommand2.Parameters.AddWithValue("@bestID", bestelID.Text);
                  updateCommand2.ExecuteNonQuery();
                  sqlConn2.Close();

                    try
                    {
                        MailMessage mail = new MailMessage();
                        mail.To.Add(emadr.ToString());
                        mail.Bcc.Add("SomeoneBcc");
                        mail.From = new MailAddress("FromWho");
                        mail.Subject = "SomeSubject";
                        string Body = LabelSendGridBoven.Text + sb.ToString() + LabelHoeveelheid.Text + LabelSendGridTussen.Text + "<br /><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >" + LabelSendGridOnder.Text + "<BR>";

                        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
                        LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\logo\Logo.jpg");
                        imagelink.ContentId = "imageId";
                        imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                        htmlView.LinkedResources.Add(imagelink);
                        mail.AlternateViews.Add(htmlView);
                        SmtpClient smtp = new SmtpClient("..server..");
                        smtp.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
        }
    }

This results in 1 email to each email account which was checked with all rows, but I would like only the rows corresponding with the email address.

Anyone an idea please?

Dieter
  • 401
  • 1
  • 9
  • 31
  • Why are you equating **lst.Contains(emadr)** will be a boolean val true or false , try just **if (lst.Contains(emadr))** – V4Vendetta Apr 20 '11 at 07:57

2 Answers2

0
lst.Contains(emadr).ToString() == emadr.ToString()

Well lst.Contains() returns a bool, which you convert to a string, so unless your email looks like true or false it's not going to work

Why not just use:

if(lst.Contains(emadr))
{...}
Kenneth Jakobsen
  • 458
  • 3
  • 11
  • Yes, my bad for not noticing that it returns a boolean. But the if statement i cannot use because it doesn't return the gridview in the email since i'm using `if (ck.Checked && !lst.Contains(emadr)){}` to make sure only 1 email is send to each email account. – Dieter Apr 20 '11 at 08:09
  • You are probably populating your gridview in page load right? if yes the checked checkboxes will never be true because they are repopulated before any(most) events are fired – Kenneth Jakobsen Apr 20 '11 at 08:47
  • The gridview that gets filled with data from the gridview on the webpages gets populated in a buttonclick method. – Dieter Apr 20 '11 at 09:56
0

I think you are doing something wrong here

if (lst.Contains(emadr).ToString() == emadr.ToString())
  {...}

lst.Contains(emadr).ToString() would be true or false and will never match the email address in the memeber emadr

From the comments as mentioned you should use

if (lst.Contains(emadr))
{
//your logic
}
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • Yes, my bad for not noticing that it returns a boolean. But the if statement i cannot use because it doesn't return the gridview in the email since i'm using `if (ck.Checked && !lst.Contains(emadr)){}` to make sure only 1 email is send to each email account. – Dieter Apr 20 '11 at 08:10
  • The scenario is not understood can you explain a bit more – V4Vendetta Apr 20 '11 at 08:51
  • Hey, sorry if it's bad explained, how it works is as followed. I have a webpage with a gridview on it. I select some rows via checkboxes then I press a button and want to send each row (also in gridview which is filled via this datatable) to the corresponding email address. But only 1 email per address, even if there are more rows selected. I have this email address in a column in the gridview on the webpage and I store it in the string emadr and I keep record of the email addresses via a List in which I also store the addresses. – Dieter Apr 20 '11 at 09:41
  • In this `if (ck.Checked && !lst.Contains(emadr)){}` I declare to who send the email and i also do `lst.Add(emadr);` here. – Dieter Apr 20 '11 at 09:43
  • So now your problem is the checked property is always false.. rite ? If so please check whether you are binding the first grid inside **If(! IsPostBack)** check – V4Vendetta Apr 20 '11 at 09:46
  • Should I upload my full method for button_click here? Even though it's pretty long? I don't really get where to post this If statement. – Dieter Apr 20 '11 at 09:58
  • But is the inference that **ck.Checked** is always **false** right ? – V4Vendetta Apr 20 '11 at 10:18
  • **ck.Checked** is default false yes, but ofcourse this changes when on the webpage a row in the gridview is checked via the checkboxes. I've uploaded the full method in the hope this clears some things up. Thank you for your help. – Dieter Apr 20 '11 at 10:27
  • Why are you looping the grid again for making the datatable, you are already in a row where its checked then why loop again – V4Vendetta Apr 20 '11 at 10:41
  • I tried this out. When I delete the foreach statement and the if statement which is in it, I receive only 1 row at each email account, I selected 2 rows for 1 email address in the gridview and it returned only the upper row. If I delete only the if statement I receive every single row in the email. Also those who aren't checked, but only at the selected email addresses though. – Dieter Apr 20 '11 at 10:54
  • Yeah ..you could check the checked property early .. rather than checking chk != null – V4Vendetta Apr 20 '11 at 10:58
  • I've done as you suggested. But the problem remains, I can't seem to add a 2nd row assigned to an email address to the gridview that I will send in an email. I have updated my code again to what it is at the moment. – Dieter Apr 20 '11 at 12:00
  • So how many rows do you want to send to say the first row is checked ? – V4Vendetta Apr 20 '11 at 12:10
  • If I say somewhere that first row is checked, could you tell me where because I don't see what I'm doing wrong. I tried adding `if (lst.Contains(emadr)){ dr = dt.NewRow(); ..rows.. }` in the `if(ck.Checked)` but this also won't work. I'm trying to check for example 3 rows with the same email address, then I would like to receive just 1 email with these 3 rows in it. – Dieter Apr 20 '11 at 12:41
  • I also had a thought of adding something like `foreach (emadr in GridViewBestelling.Columns){...}` but that syntax won't work and I don't know if this is even possible or would work. – Dieter Apr 20 '11 at 12:50