I need help. I have a gridview that updates certain fields. Once those fields are updated a button is clicked and an email needs to be sent to different areas depending on certain conditions but only if there is an email address for that row. There could be only 2 rows in the gridview or there could be 30 rows. If an email address is filled in on those rows, then an email needs to be sent. The email address could be different for each row. Some rows will not have an email address therefore an email does not need to be sent out. Then maybe one the email is sent out a check can automatically be checked indicating that an email has been sent. I know I have to do some sort of loop, but I am not good with loops. Can someone please help me.?

Recommended Answers

All 4 Replies

You should probably do something to this effect. This is just psuedocode, but you should be able to fill in the actual code. I am assuming the email address in the row has its own cell. And there might be better ways to do this, I am doing this on the fly off the top of my head.

OnButtonClick()
{
    While(GridView Has Rows)
    {
        -Create array or list to store used emails
        -Get email address from cell in current row, if null, move to next row if there is one
        -Check array for email address to see if its been sent an email already
        -If not in array, add to array, send email
        -If in array, dont send email, move to next row if there is one
    }
}

Yeah, I knew it would be something like this, but I don't know how to write the code for it.

First you need your onButtonClick event function. Then you put all the rest of the code inside it. I would actually use a Foreach loop for going through the rows of the gridview. I am not sure exactly how to get the email sent using C#, haven't had the pleasure of that part in my experience so far. I am sure you can find help online for a function to send emails. I would just write another function seperate that completely handles sending the email each time a valid address is encountered and call it where my comment is in the code. Also, I did not test this code or write it VS2010, just wrote it freehand on here, so my apologies if there are errors. I hope it gets you pointed into the right direction.

//Function that executes when button is clicked.
public void button1_OnClick(object sender, EventArgs e)
{
    string[] array1 = new string[];
    int arrayCounter = 0;

    Foreach(GridViewRow row in GridView1.Rows)
    {
         string emailAddress = row.cell[0].Text;

         if(isInArray(emailAddress, array1))
         {
             break;
         }
         else 
         {
             //send email code here.

             array1[arrayCounter] = emailAddress;
             arrayCounter++;
             break;
         }
         
    }
}

//Function to check if email is in array
public bool isEmailInArray(string emailAddress, string[] array1)
{
     bool isInArray;
     if(array1.Contains(emailAddress))
     {
          isInArray = true;
     }
     else
     {
          isInArray = false;
     {
     return isInArray;
}

I figured it out. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.