Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to ask a question, I want to keep tracking the quantity in the database every 15 seconds or so.. It works fine, but the problem is it check the every column that have quantity less than 5, not single column of quantity that have less than 5. I have the database like the image below:

First Image

And from the image below, I minus the Quantity from the below image to the database (above image), so the quantity in the database (above image) is now 2, whenever the Quantity in first and second row (below image) are less than 5, it will show the box in the bottom right corner like the below image:

Second Image

The problem is, either the quantity in the first or second row of the database still more than 5 or equal (for example: the quantity in the first row of the database is 2, but in the second row is 50), the box in the bottom right corner like above image does not show, it only shows when the quantity in first and second row in the database less than 5.

My question is: How do I show the box whenever either the quantity in the first or second row more than 5?

Here is the code that I am using:

System Manager class:

C#
public static void GetQuantity()
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                string query = "SELECT [Quantity] FROM [Database]";

                connection.Open();

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int quantity = (int)reader["Quantity"];

                            UserInformation.Quantity = Convert.ToDecimal(quantity);
                        }

                        reader.Close();
                    }
                }

                connection.Close();
            }
        }

        public static void CheckQuantity(CustomToolTip _customToolTip, IWin32Window _window, int _x, int _y, int _duration)
        {
            GetQuantity();

            string message = string.Empty;

            string productCode = string.Empty;

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                string query = "SELECT [ProductCode] FROM [Database] WHERE [Quantity] = @Quantity ORDER BY [ProductCode] ASC";

                connection.Open();

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    command.Parameters.Add("@Quantity", OleDbType.Decimal);
                    command.Parameters["@Quantity"].Value = UserInformation.Quantity;

                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            productCode = (string)reader["ProductCode"];

                            if (UserInformation.Quantity < 5)
                            {
                                message += "- Product Code: " + productCode + "\n- Quantity: " + UserInformation.Quantity + "\n\n";
                            }
                        }

                        if (message != string.Empty)
                        {
                            SystemManager.SoundEffect(@"\Media\Speech Off.wav");

                            string _message1 = "The system has detected the following: \n\n";
                            string _message2 = "Have quantity less than 5.\nPlease update them immediately.";

                            _customToolTip.Show(_message1 + message + _message2, _window, _x, _y, _duration);
                        }

                        reader.Close();
                    }

                }

                connection.Close();
            }
        }


User Information class:

C#
public static decimal Quantity
        {
            get;
            set;
        }


Main System class:(Here is where I called the box and minus the quantity from this class to the database)

C#
int timeLeft = 15;

Timer _timer = new Timer();

void MainSystem_Load(object sender, EventArgs e)
{
   _timer.Interval = 1000;

   _timer.Tick += Timer_Tick;

   _timer.Start();
}

void Timer_Tick(object sender, EventArgs e)
         {
             timeLeft--;

             if (timeLeft == 0)
             {
                 _timer.Stop();

                 MessageBox.Show("The timer has been stopped");

                 SystemManager.GetQuantity();

                 if (UserInformation.Quantity < 5)
                 {
                     MessageBox.Show("The quantity less than 5");

                     SystemManager.CheckQuantity(customToolTip1, this, _screen.Right, _screen.Bottom, 5000);

                     timeLeft = 15;

                     _timer.Start();
                 }

                 else if (UserInformation.Quantity >= 5)
                 {
                     MessageBox.Show("The quantity more than 5 or equal");

                     timeLeft = 15;

                     _timer.Start();
                 }

                 else
                 {
                     MessageBox.Show("Nothing");

                     timeLeft = 15;

                     _timer.Start();
                 }

             }
         }


Your answer much appreciated!

Thank you so much!
Posted
Updated 29-Dec-14 3:11am
v2
Comments
BillWoodruff 29-Dec-14 10:04am    
Note: I am too tired to study your code, but isn't there a way to access the Columns one-by-one and then stop the enumeration of the columns when you first the first one with the Quantity below your threshold value ? Please ignore this comment if I misunderstand you.

1 solution

I would get rid of the GetQuantity function and use the following sql...
SQL
SELECT [ProductCode], Count(Quantity) as QuantityCount FROM [Database] Group by ProductCode;


You will be able to check both the productCode and the Quantity in a single function.

I don't believe you need the order by in your sql correct?
 
Share this answer
 
Comments
Maciej Los 29-Dec-14 11:55am    
+5!
ridoy 29-Dec-14 12:47pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900