Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:07:56+00:00 2026-06-17T06:07:56+00:00

I have this small method that pops up a message box warning , problem

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:07:57+00:00Added an answer on June 17, 2026 at 6:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small method that looks like this: public static void unstarTrack(Context ctxContext,
I have this small class called City that simply holds some information about a
I have this small PHP script that takes a POST variable and writes it
I have this small piece of code that basically takes a list and runs
I have this small ruby script that collects data and then save them in
I am a Python newbie. I have this small problem. I want to print
I have this small snippet of python code that I wrote. It works, but
So I have this reaaaally long method that I want to test. I could
So, I have this small bit of code that generates rows and columns of
I have a small method I use to disable my socket that listens for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.