Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
problem

How to allow concurrency when multi user work on same windows form ?


I work on visual studio 2015 windows form application csharp

I have table name Employee on sql server 2012 have three fields

Serial int pk(primarykey)

EmployeeName nvarchar(100)

Address nvarchar(100)

Tel nvarchar(100)

I have windows form FrmEmployee

This windows form may be access from multi users on same time may be 5 user

When these five users open windows form frmemployee on same time

If two users insert same record on same time it will make violation primary key so That how to prevent that

from happen

Example

IF I have 3 users A,B,C

Suppose in table Employee last serial or max serial is 5

FrmEmployee windows form have textbox Serial

WHEN user A open windows form FrmEmployee then serial will be get next as 6 this is no problem for one user

but if user A open windows form and serial be 6 then

on same time

User B open windows form FrmEmployee and serial will be 6

When add data user A will add record success

But B will give him violation primary key

SO that

How to solve this problem please ?

What I have tried:

if user A open windows form and serial be 6 then 

on same time

User B open windows form FrmEmployee and serial will be  6 

When add data user A will add record success

But B will give him violation primary key
Posted
Updated 7-Jul-19 18:42pm

1 solution

Simple: you are doing the "next value" assignment yourself, and almost certainly "pre-assigning" the value - as in you assign the "next Employee serial" before you add it to the DB (probably when the form is opened, so it's ready for when it's used).

That's a bad idea: the earlier you create it, the more likely you are to have problems. Create it when the form is opened (or immediately after the last one is sent to the DB) and you will get the problem very, very often; create it immediately before you send the data to the DB and you will get it sometimes, but the frequency will increase as your user count goes up.

The only way to do that is to assign it as the data is committed to the DB - and the best way to do that is to let the DB handle it by using an IDENTITY column.

You don't need it before you create the DB row, even if you think you do - you can't use it to cross reference information or look anything up until the Employee exists!
 
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