Click here to Skip to main content
15,887,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Desktop Application in c#
3 client computer over LAN.
i am using sql max function to get maximum voucher number from table1.
now let suppose,

PC 1 - user - ABC : Using Receipt Form
PC 2 - user - PQR : Using Receipt Form
PC 3 - user - XYZ : Using Receipt Form

due to sql max function , i am getting a number say 15. i have show it on receipt form. now All users are getting Receipt No. 15.

now when they press save button at same time what will happen?
how can i solve this.

is there any way to create a sql queue even user pressing save button at same time.

thanks.
Posted
Comments
Tomas Takac 28-Jan-16 5:06am    
Assuming you are using SQL Server. If this is primary key, then you should use identity[^]. If this a unique business key, then store it in dedicated table and increment each time you create a new receipt. Or even better use a sequence[^].

1 solution

If you only need the voucher code after the voucher is saved then you can use an identity column.

You can then access the value assigned as part of the insert using the scope_identity function.

SQL
DECLARE @VoucherId INT
INSERT INTO dbo.Vouchers (Foo, Bar) VALUES (@Foo, @Bar)
SELECT @VoucherId = SCOPE_IDENTITY()


If you have a requirement to display a voucher code before it's written to the database you can use a counter table.

To ensure there are no clashes between concurrent requests for counter values you perform a variable assignment as part of the update statement which increments the counter.

This process simulates what, as suggested by Tomas, a sequence [^]does but has better support for older versions of SQL.

SQL
CREATE TABLE Counters
(
	CounterType TINYINT,
	CounterValue BIGINT
)

DECLARE 
	@CounterType TINYINT,
	@CounterValue BIGINT
	
UPDATE
	dbo.Counters
SET
	@CounterValue = CounterValue,
	CounterValue = CounterValue + 1
WHERE
	CounterType = @CounterType
 
Share this answer
 

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