Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to have a message box pop up only once in a loop?
    primarykey
    data
    text
    <p>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).</p> <p>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?</p> <pre><code> 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 &gt; 4.5) { flag = true; } if (flag &amp;&amp; !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 &gt; 4.5) { flag = true; } if (flag &amp;&amp; !shown) { string shortmsdsname = Convert.ToString(dt.Rows[0]["Name"]); string msg = "Short Msds " + shortmsdsname + " must be updated"; MessageBox.Show(msg); shown = true; } } con.Close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload