I am using ASP.NET 4.0 with SQL Server 2008 R2 and PetaPoco ORM.
A Web Form consists of Tab Panels, for example:
— Employee General Information
— Appointment
— Education
Each Tab Panel entry goes to a specific table in SQL Server. I have a main table, employee, with Primary Key empID. Other tables, viz., appointment, education etc. are related with empID.
I have multiple methods to Save record in respective tables:
AddGeneralInformation saves the General Information Tab Panel record.
AddAppointment saves the appointment details and so on.
The application is being used in a concurrent environment where multiple users are either inserting or updating records.
For the second and third methods to insert a new record, the methods must have the correct empID. As soon as the first method (General Information) saves the record, the empID is to be used in other methods.
The problem is that if I use:
Select max(empID)
then it won’t pick the correct empID as many users are inserting records.
On solution I feel is to use another column containing SessionID and use this query:
Select max(empID) where sessionID = SessionID
Is there any more reliable way to do this?
** Edited **
protected void btnSave_Click(object sender, EventArgs e)
{
Session.Add("TaskFlag", "New");
AddUpdateEmployee();
AddUpdateAddress();
}
protected void AddUpdateEmployee()
{
var db = new PetaPoco.Database("cnWeb");
var emp = new employee(); */
using (TransactionScope scope = new TransactionScope())
{
db.BeginTransaction();
emp.deptcode = txtEmpCode.Text.TrimEnd();
emp.empname = txtEmpName.Text.TrimEnd();
emp.guardianname = txtGuardian.Text.TrimEnd();
emp.relationwithemployee = ddlRelation.Text.TrimEnd();
emp.gender = ddlGender.SelectedItem.Text;
emp.dateofbirth = Convert.ToDateTime(txtDOB.Text.TrimEnd());
if (Session["TaskFlag"].ToString() == "New")
db.Insert(emp);
else if (Session["TaskFlag"].ToString() == "Update")
db.Update<employee>("SELECT * FROM employee WHERE empid = @0", Session["EmployeeID"]);
reuse.CustomClientMessage("Record Saved", this.Page);
ClearFields();
/* Commit Transaction */
db.CompleteTransaction();
scope.Complete();
}
}
protected void AddUpdateAddress()
{
var db = new PetaPoco.Database("cnWeb");
var addr = new emp_address();
using (TransactionScope scope = new TransactionScope())
{
db.BeginTransaction();
/* Permanaent Address */
addr.perm_houseno = txtPermHouse.Text.TrimEnd();
addr.perm_street = txtPermStreet.Text.TrimEnd();
addr.perm_place = txtPermCity.Text.TrimEnd();
addr.perm_pincode = txtPermPincode.Text.TrimEnd();
addr.perm_landlinephone = txtPermLandline.Text.TrimEnd();
addr.perm_mobilephone = txtPermMobile.Text.TrimEnd();
if (Session["TaskFlag"].ToString() == "New")
db.Insert(addr);
else if (Session["TaskFlag"].ToString() == "Update")
db.Update<emp_address>("SELECT * FROM emp_address WHERE empid = @0", Session["EmployeeID"]);
/* Commit Transaction */
db.CompleteTransaction();
scope.Complete();
}
}
Best solution: make your
EmpIDcolumn anINT IDENTITYin SQL Server and let the database handle the details.Anything you dream up in code (T-SQL or C#) is most likely going to have some issues and won’t work well under load – why not just use what’s there, works, and is tested by hundreds of thousands of users?
Once you insert a row into a table with an
INT IDENTITYcolumn, you can fetch that value usingor alternatively, you could use the
OUTPUTclause on yourINSERTstatement