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 8301797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:07:50+00:00 2026-06-08T17:07:50+00:00

This is the the sample stored procedure that I am using CREATE PROCEDURE [dbo].[CreateCustomer]

  • 0

This is the the sample stored procedure that I am using

CREATE PROCEDURE [dbo].[CreateCustomer]
        @firstName nvarchar(50) = null,
        @lastName nvarchar(50) = null,
        @emailAddress nvarchar(50),
        @contactNumber varchar(15) =null,
        @street1 nvarchar(50) = null,
        @street2 nvarchar(50) = null,
        @city nvarchar(50) = null,
        @State nvarchar(50)= null,
        @Country nvarchar(50)=null,
        @postalCode varchar(5) = null,
        @middleName nvarchar(50),
        @password nvarchar(50),
        @membershipExpieryDate datetime,
        @status int OUT

    AS
        BEGIN TRY
            BEGIN TRANSACTION
            SAVE TRANSACTION initialP
                DECLARE @lastAccessed datetime = GETDATE()
                DEClARE @uniqueCustomerID UNIQUEIDENTIFIER= NEWID()
                BEGIN TRANSACTION
                    INSERT INTO Customers(CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
                        Street1, Street2, City, State, Country, PostalCode, MiddleName, Password, MembershipExpieryDate)
                        VALUES(@uniqueCustomerID,@firstName, @lastName, @emailAddress, @lastAccessed, @contactNumber, @street1, @street2,
                         @city, @State, @Country, @postalCode, @middleName, @password, @membershipExpieryDate)



     INSERT INTO Roles(CustomersCustomerID,Role) VALUES(@uniqueCustomerID,'V') 
            COMMIT

            SELECT CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
                Street1, Street2, City, State, Country, PostalCode, MiddleName, MembershipExpieryDate FROM Customers WHERE EmailAddress = @emailAddress  

        SET @status =1
        COMMIT
    END TRY 
    BEGIN CATCH
        IF(@@TRANCOUNT > 0)
        BEGIN
            ROLLBACK TRANSACTION initialP
        END
        SET @status =2
    END CATCH

And this is the code that I am using to get the query data:

ObjectParameter status = new ObjectParameter("status", typeof(int));
var dataSet = Entity.CreateCustomer(newCustomer.FirstName, newCustomer.LastName,
newCustomer.EmailAddress, newCustomer.ContactNumber, newCustomer.Street1, newCustomer.Street2,
newCustomer.City, newCustomer.State, newCustomer.Country, newCustomer.PostalCode, newCustomer.MiddleName,
newCustomer.Password, newCustomer.MembershipExpieryDate, status);

But when execiting this stored procedure it gives an error saying that Trans count missmatch.
As I know the trans count is incrimented by one when a begin transaction occurs.and decriment by one when an commit occures.
I have already map the function in the function import as it can return a complex type.

I can not understand the problem in this. To me it seems Ok. I want to know weather I am doing this correct or not with some detais. Thanks.

Edited:
This I what I found out by writing a sample code. I first thought it was with begin trans action and commit. But it is not. It allows to select the values that are been inserted even before the commit statement (Correct me if I am wrong.).
I created a Test Table which has 3 columns(test1 unique identifier, test2 int , test3 int)
and I created a stored procedure call Test:
Its given below:

CREATE PROCEDURE dbo.TestForCommit

    (
    @test11 int,
    @test111 int,
    @status int OUTPUT
    )

AS
    begin try
        declare @test1 uniqueidentifier  = NEWID()
        begin transaction
            insert into TestT1(Test1, Test11, Test111) values(@test1, @test11, @test111)
        commit
        select Test1, Test11, Test111 from TestT1 where Test1 = @test1
        SET @status = 1
    end try
    begin catch
        Rollback Transaction
        set @status =2
    end catch
    /* SET NOCOUNT ON */
    RETURN

I have created a functional import in the in the entity model and I used it like this:

            ObjectParameter status = new ObjectParameter("status", typeof(int));
            SchoolEntities test = new SchoolEntities();
            var dataSet = test.TestForCommit(1, 1, status);
                //A:
                //(XXXXXXXXXXXXX)Console.WriteLine((int)status.Value);
                foreach (var item in dataSet)
                {
                    TestForCommit_Result t = item;
                    Console.WriteLine("{0} , {1} , {2}", t.Test1, t.Test11, t.Test111);

                }
                //B:
                Console.WriteLine((int)status.Value);
                Console.ReadLine();

This //A: part is causing the problem. This gives a null reference exception. but //B: part is working and giving the status value. I am still in search of why is it working like that. Thanks!!

  • 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-08T17:07:52+00:00Added an answer on June 8, 2026 at 5:07 pm

    My Suggestion. (Set Xact_Abort On)

    Begin Try
        Set NoCount On
        Set Xact_Abort On
        Begin Tran
            --Your Query
        Commit Tran
    End Try
    
    Begin Catch
        Rollback Tran
    End Catch
    

    CREATE PROCEDURE [dbo].[CreateCustomer]
        @firstName nvarchar(50) = null,
        @lastName nvarchar(50) = null,
        @emailAddress nvarchar(50),
        @contactNumber varchar(15) =null,
        @street1 nvarchar(50) = null,
        @street2 nvarchar(50) = null,
        @city nvarchar(50) = null,
        @State nvarchar(50)= null,
        @Country nvarchar(50)=null,
        @postalCode varchar(5) = null,
        @middleName nvarchar(50),
        @password nvarchar(50),
        @membershipExpieryDate datetime,
        @status int OUT
    
    AS
    BEGIN TRY
        Set NoCount ON
        Set XAct_Abort ON
        DECLARE @lastAccessed datetime = GETDATE()
        DEClARE @uniqueCustomerID UNIQUEIDENTIFIER= NEWID()
    
        BEGIN TRANSACTION
        INSERT INTO Customers(CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
        Street1, Street2, City, State, Country, PostalCode, MiddleName, Password, MembershipExpieryDate)
        VALUES(@uniqueCustomerID,@firstName, @lastName, @emailAddress, @lastAccessed, @contactNumber, @street1, @street2,
        @city, @State, @Country, @postalCode, @middleName, @password, @membershipExpieryDate)
    
        INSERT INTO Roles(CustomersCustomerID,Role) VALUES(@uniqueCustomerID,'V') 
        COMMIT
    
        SELECT CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber, 
        Street1, Street2, City, State, Country, PostalCode, MiddleName, MembershipExpieryDate FROM Customers WHERE EmailAddress = @emailAddress  
    
        SET @status =1
        COMMIT Tran
    END TRY 
    BEGIN CATCH
        ROLLBACK TRANSACTION
        SET @status =2
    END CATCH
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this sample : I have make my own Fragment that holds tabhost and
I need to create a Postgres 9.1 PL/pgSQL stored procedure that, among other parameters,
I am trying to create a simple msmq messaging application using clr stored procedure
I’m using Microsoft Chart extensions that ship with VS 2010. This suits my needs
Based on this previous question on stackoverflow: Fetch Oracle table type from stored procedure
I am trying to create a simple stored procedure in PostgreSQL within which I
I have imported a stored procedure that returns a dataset into my Entity Framework.
I am running a simulation on financial data which fires off this stored procedure
Okay basically I am creating a stored procedure that will return data for our
I've got a stored procedure that executes some repetitive code, so I decided 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.