Search code examples
c#asp.netc#-4.0popupmessagebox

How to have a message box pop up only once in a loop?


I have this small method that pops up a message box warning , problem is that it pops up 3 message box instead of one! I've tried several ways to counter this issue (including the bool variables in the code and using Distinct in the sql query , though the database doesn't contain any repeatable rows).

The idea is to have the messagebox pop up once for each row that violates my if condition and not 3 times for each row. So , why is this message box pops up 3 times instead of once? and how to fix it?

    void msds_update()
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "server=(local);database=PhilipsMaterials;Integrated Security=SSPI;";
    con.Open();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string sql = "Select * from [PhilipsMaterials].[dbo].[Materials]";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    da.Fill(ds);
    dt = ds.Tables[0];

    DateTime longupdate;
    DateTime shortupdate;
   foreach (DataRow row in dt.Rows)
    {
        longupdate = Convert.ToDateTime(dt.Rows[0]["Long MSDS Update"]);
        shortupdate = Convert.ToDateTime(dt.Rows[0]["Short MSDS Update"]);
        TimeSpan longsince = DateTime.Now.Subtract(longupdate);
        int longyears = (int)(longsince.Days / 365.25);
        TimeSpan shortsince = DateTime.Now.Subtract(shortupdate);
        int shortyears = (int)(shortsince.Days / 365.25);
        bool flag = false ;
        bool shown = false;
        if (longyears > 4.5) { flag = true; }
        if (flag && !shown)
        {
            string longmsdsname = Convert.ToString(dt.Rows[0]["Name"]);
            string msg = "Long Msds " + longmsdsname + " must be updated";
            MessageBox.Show(msg);
            shown = true;
        }
        flag = false;
        shown = false;
        if (shortyears > 4.5) { flag = true; }
        if (flag && !shown)
        {
            string shortmsdsname = Convert.ToString(dt.Rows[0]["Name"]);
            string msg = "Short Msds " + shortmsdsname + " must be updated";
            MessageBox.Show(msg);
            shown = true;
        }
    } 
    con.Close();
}

Solution

  • The values used to perform the if tests and to build the error message always are the values from the row at index zero, you should use the current row indexer used in the foreach loop

    Also, do not react immediatly to the error condition, instead build up an error message, wait to end one loop and show the message only if there is an error. No need to use and keep updated two status variables.

       StringBuilder sb = new StringBuilder();
       int rowCounter = 0;
       foreach (DataRow row in dt.Rows)
       {
            rowCounter++;
    
            longupdate = Convert.ToDateTime(row["Long MSDS Update"]);
            shortupdate = Convert.ToDateTime(row["Short MSDS Update"]);
            TimeSpan longsince = DateTime.Now.Subtract(longupdate);
            int longyears = (int)(longsince.Days / 365.25);
            TimeSpan shortsince = DateTime.Now.Subtract(shortupdate);
            int shortyears = (int)(shortsince.Days / 365.25);
            if (longyears <= 4.5) 
            {
                string longmsdsname = Convert.ToString(row["Name"]);
                sb.AppendFormat("Long Msds {0}  must be updated\r\n", longmsdsname);
            }
            if (shortyears <= 4.5)
            {
                string shortmsdsname = Convert.ToString(row["Name"]);
                sb.AppendFormat("Short Msds {0}  must be updated\r\n", shortmsdsname);
            }
    
            // If we have errors, show them and reset the builder for the next loop
            if(sb.Length > 0)
            {
                 string msg = string.Format("Error in row {0}\r\n{1}", 
                                             rowCounter.ToString(), sb.ToString());
                 MessageBox.Show(msg);
                 sb.Length = 0;
            }
        }
    

    In this way you have just one message for each wrong row also if there are 2 or more wrong values.