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!!
My Suggestion. (Set Xact_Abort On)