0

I want to increase the number that the table in the database reached to -example:

My [CustomerNo] reached 50, and I close the app, and open it again, I want it to complete from 50(++), and I want from the numbers to start again from 1 if they reached 250.

Globals.order++; is what i use .

Here is my code:

private void Order()
        {
            using (SqlConnection connection = new SqlConnection(connectionString1))
            {
                String query = "INSERT INTO Tbl_order (OrderName,Quantity,Price,Serves_way,Date,CustomerNo) VALUES (@OrderName,@Quantity, @Price,'" + servers + "','" + time1.ToString(format1) + "','" + Globals.order + "' )";
                Globals.order++;

                connection.Open();
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    // Add the length of this text column as third parameter...
                    command.Parameters.Add("OrderName", SqlDbType.NVarChar, 50);
                    command.Parameters.Add("Quantity", SqlDbType.Int);
                    command.Parameters.Add("Price", SqlDbType.Money);
                    command.Prepare();

                    for (int i = 0; i < lst_OrderName.Items.Count; i++)
                    {
                        command.Parameters[0].Value = lst_OrderName.GetItemText(lst_OrderName.Items[i]);
                        command.Parameters[1].Value = Convert.ToInt32(lst_QTY.Items[i]);
                        command.Parameters[2].Value = Convert.ToDouble(lst_Price2.Items[i]);

                        command.ExecuteNonQuery();
                    }
                }
            }
        }

enter image description here

But when I close the app, it restart from 0 again.

Ilyes
  • 14,640
  • 4
  • 29
  • 55

2 Answers2

0

You get the last used record with:

select customerno
from tbl_order
order by date desc
limit 1;

If this returns 50, then the next number to use will be 51. It is not clear, however, whether you always insert new records or if you would at one time have 250 records in the table and start updating old ones. If the latter is the case you may want to use INSERT ... ON DUPLICATE KEY UPDATE.

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73
  • what i want it just to get the last number from the table like if i reached 50 and close my C# app and open it , it will complete from the last number it reached to (50) and by using C# i will use a code like the Globals.order++ to increase that number . – Max Thunder Sep 19 '17 at 17:26
0

If you want a value to be kept after you close the app, you may need to store it on the database.

What is the purpose of your Globals.order++?

If it's to increment the order number, you should use something on the database such as an AUTO INCREMENT column on the table. This means that whenever a new value is inserted, the value is increased by 1. You can then use code to find the most recently added number.

If you want it to reset at a certain point, MySQL doesn't have sequences but this might be helpful:

How do I create a sequence in MySQL?

Does that make sense?

bbrumm
  • 1,342
  • 1
  • 8
  • 13
  • i know that ^^ but what i'm asking is that i want know how to get the last number from the table like if i have 48,49,50 and i close my app and opened it again it will complete 51,52... like that . Globals.order++ is a C# code to increase the number – Max Thunder Sep 19 '17 at 17:20