deepak veer

deepak veer

  • NA
  • 15
  • 943

Transaction Scope inserting duplicate values. How to lock se

Mar 3 2015 2:38 AM
i am using transaction scope in my project. Isolation level is IsolationLevel.ReadCommitted <br/>
i Also tried IsolationLevel.RepeatableRead
using (DBOperations dboperation = new DBOperations())
{
GetAccountVoucherNuber();
}
i am calling this method from Transaction Scope.
private void GetAccountVoucherNuber() {
var _dB0010013 = _dB0010013Repository.GetAll().Where(e = > e.Category == "D" && e.CategoryType == "TSNO" && e.Branch == Branch && EntityFunctions.TruncateTime(e.OnDate) == EntityFunctions.TruncateTime(TranInit.EntryDate) && e.CodeOne == BatchCode).FirstOrDefault();
if (_dB0010013 == null) {
_dB0010013 = new DB0010013();
_dB0010013.Branch = Branch;
_dB0010013.Category = "D";
_dB0010013.CategoryType = "TSNO";
_dB0010013.CodeOne = BatchCode;
_dB0010013.CodeTwo = "";
_dB0010013.CodeThree = "";
_dB0010013.OnDate = TranInit.EntryDate;
_dB0010013.Note = "";
_dB0010013.LastNo = 1;
var _operationStatus = _dB0010013Repository.AddAndSave(_dB0010013, false);
} else {
_dB0010013.LastNo += 1;
var _operationStatus = _dB0010013Repository.UpdateAndSave(_dB0010013, false);
}
}
<br>when two or more user submits the page at same time.<br> i am getting same no for two transaction.
<br>
eg. suppose user1 got lastNo =85 +=1 =86
<br> at the same time user2 also get lastNo =85 +=1 =86
<br> so the same no is applied for two different transactions.
<br>
How Can i Lock Select Statement after getting values. or what is way to handle it.<br>i googled a lot but didnt find any solution.<br>

Answers (1)