0

I have a .NET application written in C#, as well as i am using mysql database,

the application is installed on multiple devices all these devices connected to MySQL database server, here is what i am facing:

Two users opened the application on the screen they have same invoice number like 123 ONCE they save their order the application will check if the number already stored before, if yes the 123 will be incremented by 1 other wise the application will keep the same number,

the problem is that when the users click the save button somehow in the same time, both invoices will have the 123 at the same time with different data.. how can i solve this once and forever. ? thanks for your help.

the code that checks the invoice sequence:

                if (accounts_transactionsTableAdapter.getLastTransNumber(4) != null)
                {
                    textBox1.Text = ((long)(accounts_transactionsTableAdapter.getLastTransNumber(4) + 1)).ToString();
                }
                else
                {
                    textBox1.Text = "1";
                }

4 is the transaction type which represent sale invoice, getLastTransNumber() is a stored procedure that checks the last invoice number of type (4) which is sales

Alpha Con
  • 15
  • 3
  • 1
    Why is the application creating two invoices with the same number? Why is it creating a number at all before the invoice has been saved? – David Aug 20 '15 at 12:49
  • It would be helpful if you posted your code. More specifically what method is creating the record on the db. – Leonardo Wildt Aug 20 '15 at 12:51
  • when you open invoicing form the application will query for the last stored invoice number, it will find the 123 so it will bring it on the screen so if another user open same form from the another computer the check process will be the same but user 1 still working on the first invoice so both will get the same number. – Alpha Con Aug 20 '15 at 12:53

1 Answers1

1

Probably you need to implement an Optimistic Locking strategy.

Community
  • 1
  • 1
lifeisfoo
  • 15,478
  • 6
  • 74
  • 115