Kuriyama Mirai
Kuriyama Mirai

Reputation: 937

Entering Query at the same time c#

I have a C# application in which one or many users can transact. Once a transaction is being done, the application provides a number depending on the maximum number of data entered in the database. It works fine when only one user is executing a transaction, but, when there is more than one user and I hit the submit button at the same time, all the users receive the same number.

What I want to do is that users do not receive the same number when they hit the submit button at the same time.

My Query to get the number is:

  SqlCommand cmd5 = new SqlCommand("Select Max(q_num) FROM [NUMBER] Where CONVERT(VARCHAR(10),g_time,110) = '" + DateTime.Now.ToString("MM-dd-yyyy") + "'", con);

It gets the maximum number of transactions per day. How can I make that even when all the users hit the submit button at the same time? User 1 will get number 1, user 2 will get number 2 and so on.

Can anyone help me out? Any Idea how can I possibly do it?

Upvotes: 0

Views: 123

Answers (2)

masum7
masum7

Reputation: 832

If you don't want to use identity column as it resets at day end, you can create another table for holding the identity column.

I mean, a side-table will hold the identity column and you will insert and get identity value from that table. At day end, you can run a task to truncate the side-table.

Upvotes: 0

sgeddes
sgeddes

Reputation: 62861

Sounds like you should have a stored procedure that enters the transaction and returns the Inserted.q_num field.

Something like:

create procedure transaction_add
    @customerid int,
    @othermetadata varchar(100)
as
    insert into sometable (customerid, othermetadata)
    output Inserted.q_num
    values (@customerid, @othermetadata)
go

Upvotes: 2

Related Questions