Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7962343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:18:36+00:00 2026-06-04T05:18:36+00:00

I am using ASP.NET 4.0 with SQL Server 2008 R2 and PetaPoco ORM. A

  • 0

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();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:18:37+00:00Added an answer on June 4, 2026 at 5:18 am

    Best solution: make your EmpID column an INT IDENTITY in 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 IDENTITY column, you can fetch that value using

    DECLARE @NewEmpID INT
    SELECT @NewEmpID = SCOPE_IDENTITY()
    

    or alternatively, you could use the OUTPUT clause on your INSERT statement

    INSERT INTO dbo.YourTableHere(List of columns)
    OUTPUT Inserted.EmpID
    VALUES(list of value here)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an asp.net web application written in C# using a SQL Server 2008
I am developing one web application using asp.net 2005 and Microsoft SQL Server 2008
We are building ASP.NET MVC3 web applications using Visual Studio, SQL Server 2008 R2
I have an ASP.NET Web Application using SQL Server 2008. When a post code
I'm developing an ASP.NET app (C#) that connect to SQL Server 2008 using ADO.NET
Example scenario in an ASP.NET application using SQL Server membership provider : 1) a
I maintain a legacy ASP.Net Web application (using .Net 2.0 + SQL Server 2005
What are the licences required for an asp.net (MVC) web application using sql server
using asp.net, sql server 2008, winserver2003 we have about 10 tables with about 20
I am using SQL Server 2008 in a asp.net/c# program. I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.