-3

basically... When the user accepts or rejects a friend request it is supposed to remove the user's name, accept and reject button but it only removes the user's name and reject button. I don't understand. Code:

        private void loadFriendRequests()
    {
        using (SqlConnection connection = new SqlConnection(con))
        {
            using (SqlCommand cmd = new SqlCommand(@"Select IDRequest, UserFirstName, UserLastName, FriendEmail From PendingRequests Where FriendEmail = @fe", connection))
            {
                connection.Open();
                cmd.Parameters.AddWithValue("@fe", Properties.Settings.Default.Email);
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    int i = 0;
                    while (dr.Read())
                    {
                        i++;
                        foreach (object request in i.ToString())
                        {
                            Label userName = new Label();
                            Button accept = new Button();
                            Button reject = new Button();
                            accept.Text = "Accept";
                            reject.Text = "Reject";
                            int idRequest = Convert.ToInt32(dr["IDRequest"]);
                            userName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(dr["UserFirstName"].ToString() + " " + dr["UserLastName"].ToString());
                            userName.Tag = idRequest;
                            accept.Tag = idRequest;
                            reject.Tag = idRequest;

                            accept.Click += Accept_Click;
                            reject.Click += Reject_Click;

                            friendRequestPanel.Controls.Add(userName);
                            friendRequestPanel.Controls.Add(accept);
                            friendRequestPanel.Controls.Add(reject);
                        }
                    }
                }
            }
        }
        Requests.Start();
    }
    private void Reject_Click(object sender, EventArgs e)
    {
        Button c = sender as Button;
        int idRequest = Convert.ToInt32(c.Tag);
        var ctrls = friendRequestPanel.Controls
                                      .Cast<Control>()
                                      .Where(x => 
                                             Convert.ToInt32(x.Tag) == idRequest);
        foreach (Control ct in ctrls)
        {
            friendRequestPanel.Controls.Remove(ct);
            ct.Dispose();
        }
        updateFriendRequestDatabase(2);
    }
    private void Accept_Click(object sender, EventArgs e)
    {
        Button c = sender as Button;
        int idRequest = Convert.ToInt32(c.Tag);
        var ctrls = friendRequestPanel.Controls
                                      .Cast<Control>()
                                      .Where(x => x.Tag != null &&
                                             Convert.ToInt32(x.Tag) == idRequest);
        foreach (Control ct in ctrls)
        {
            friendRequestPanel.Controls.Remove(ct);
            ct.Dispose();
        }
        updateFriendRequestDatabase(1);

    }

Picture: GUI

When any of the buttons are clicked: GUI

Why isn't it deleting the 'Accept' button?

richardj97
  • 79
  • 1
  • 10
  • 2
    have you actually placed breakpoints in the code and stepped through to see where the expected is failing.. please do that first you can't expect us to code review this for you based on all the code you have posted.. thank you – MethodMan Jun 21 '16 at 20:29
  • 2
    "Here's my code, fix it" – LaneL Jun 21 '16 at 20:31
  • 1
    Agreed. The first question I asked myself was, "did the OP debug his own code?" Is your accept button in the `ctrls` enumeration? –  Jun 21 '16 at 20:31
  • it's like saying `I don't understand why I didn't win the Lottery win you never purchased a ticket in the first place..` – MethodMan Jun 21 '16 at 20:32
  • what is Tag of accept button after first function? Maybe it's null? – Whencesoever Jun 21 '16 at 20:34
  • Yes, attaching the event handlers in the loop is fine. –  Jun 21 '16 at 20:36
  • Amy, I put breakpoints in and its deleting username, reject button instead of usrname, accept button, reject button. I don't understand :( – richardj97 Jun 21 '16 at 21:18

1 Answers1

1

You are changing the collection during the loop. To solve the problem, you can call ToList at the end of the criteria which you find controls, and loop over the result. This way, you are looping through a different list than the collection you want to change:

var ctrls = friendRequestPanel.Controls.Cast<Control>()
                              .Where(Convert.ToInt32(x.Tag) == idRequest)
                              .ToList();  //<--- Creates a new List<Control>
foreach (Control ct in ctrls)
{
    friendRequestPanel.Controls.Remove(ct);
    ct.Dispose();
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • **Note for OP:** When you post a lot of code which is also related to database, it's really hard to find the problem, because the problem may be because of any part and no one can reproduce the problem simply. The question would receive more attention and more effective helps if you post [mcve](http://stackoverflow.com/help/mcve). A well formatted question will be more useful for future readers too :) – Reza Aghaei Jun 21 '16 at 22:02
  • Thank you Reze Aghaei, I spent hours trying to fix it, I did try the 'ToList()' but it didn't work, but it is working now :) thanks a lot man. – richardj97 Jun 22 '16 at 14:29