Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

I have finyearwise data in finyearhosteldetails table. But i not able to update them Whenever i click button update it is giving me the following error.

transaction count after execute indicates a mismatching number of begin and commit statements. previous count = 1, current count = 0

How to solve this?? Please give the solution as it under top priority

My sp is:

SQL
ALTER PROCEDURE [dbo].[UpdateBasicHostelDetails] 
  (     
  @DistrictCd int,   
  @TalukCd int,  
  @VillageCd int,   
   @HostName varchar(150),  
    @PreOrPost int,   
    @ScOrSt varchar(10),   
    @BoysOrGirlsOrCoEd int,  
     @Sant int,   
     @Admt int,   
     @WardenName varchar(100),   
     @WardenMobNo float ,   
     @WardenMobNo2 float,   
     @NeighborName1 varchar(100),  
     @Neigh1MobNo float,   
     @NeighborName2 varchar(100),  
      @Neigh2MobNo float,   
      @Location varchar(150),   
      @HostelAdd varchar(500),   
      @User varchar(25),   
      @FinYear varchar(25),   
      @Ip varchar(150),   
      @HostelCd  int,
      @seccode nvarchar(max),
      @plan nvarchar(max)
)
as
begin 

	begin try 
	begin transaction   
	DECLARE @Count int
	
	update HostelDetails 
	set districtcd=@DistrictCd,talukcd=@TalukCd, HostelName=@HostName ,Location=@Location ,Hostel_Address=@HostelAdd , 
	Sc_St_Hostel=@ScOrSt,B_G_Coed=@BoysOrGirlsOrCoEd,Pre_Post=@PreOrPost ,
	Sanctioned=@Sant ,Admitted=@Admt ,Warden_Name=@WardenName ,Warden_Mobile_No1=@WardenMobNo ,
	Warden_Mobile_No2=@WardenMobNo2 ,Neighbor1_Name=@NeighborName1 ,Neighbor1_MobNo=@Neigh1MobNo ,
	Neighbor2_Name=@NeighborName2 ,Neighbor2_MobNo=@Neigh2MobNo ,status=1,Sector=@seccode,HostelPlan=@plan   
	where Hostelcd=@HostelCd


	--update  FinYearHostelDetails 
	--set HostelName=@HostName ,Location=@Location ,Hostel_Address=@HostelAdd , 
	--Sc_St_Hostel=@ScOrSt,B_G_Coed=@BoysOrGirlsOrCoEd,Pre_Post=@PreOrPost ,
	--Sanctioned=@Sant ,Admitted=@Admt ,Warden_Name=@WardenName ,Warden_Mobile_No1=@WardenMobNo ,
	--Warden_Mobile_No2=@WardenMobNo2 ,Neighbor1_Name=@NeighborName1 ,Neighbor1_MobNo=@Neigh1MobNo ,
	--Neighbor2_Name=@NeighborName2 ,Neighbor2_MobNo=@Neigh2MobNo ,status=1, Fin_year=@FinYear,Sector=@seccode,HostelPlan=@plan   
	--where Hostelcd=@HostelCd
		
	set @Count =(select count(*) from  FinYearHostelDetails where Hostelcd =@HostelCd and (Fin_year=@FinYear))     
	if (@Count = 0)          
	Begin            
	insert into FinYearHostelDetails(districtcd,talukcd,Hostelcd,HostelName,Sc_St_Hostel,B_G_Coed,Pre_Post, 
	Sanctioned,Admitted,Warden_Name,Warden_Mobile_No1,Warden_Mobile_No2,Neighbor1_Name,
	Neighbor1_MobNo,Neighbor2_Name,Neighbor2_MobNo,Location,Hostel_Address,Status,
	Fin_year,Sector,HostelPlan)            
	values (@DistrictCd,@TalukCd,@HostelCd,@HostName ,@ScOrSt ,@BoysOrGirlsOrCoEd ,@PreOrPost ,@Sant ,@Admt ,@WardenName ,
	@WardenMobNo ,@WardenMobNo2 ,@NeighborName1 ,@Neigh1MobNo ,@NeighborName2 ,@Neigh2MobNo ,@Location ,
	@HostelAdd ,0,@FinYear,@seccode,@plan)                                        
	End      
	Else        
	Begin           
	update  FinYearHostelDetails 
	set districtcd=@DistrictCd,talukcd=@TalukCd, HostelName=@HostName ,Location=@Location ,Hostel_Address=@HostelAdd ,
	Sc_St_Hostel=@ScOrSt ,B_G_Coed=@BoysOrGirlsOrCoEd ,Pre_Post=@PreOrPost , 
	Sanctioned=@Sant ,Admitted=@Admt ,Warden_Name=@WardenName ,Warden_Mobile_No1=@WardenMobNo ,
	Warden_Mobile_No2=@WardenMobNo2 ,Neighbor1_Name=@NeighborName1 ,Neighbor1_MobNo=@Neigh1MobNo ,
	Neighbor2_Name=@NeighborName2 ,Neighbor2_MobNo=@Neigh2MobNo ,status = 0,Fin_year=@FinYear,Sector=@seccode,HostelPlan=@plan           
	where  Hostelcd =@HostelCd                               
	 End   
	 commit transaction    
	 End Try
Begin Catch    
BEGIN        
ROLLBACK TRANSACTION    
END	
End catch
end
Posted
Updated 5-Aug-15 20:11pm
v2
Comments
Maciej Los 6-Aug-15 2:34am    
It's happening because you're trying to update your table twice!

Please, read my comment to the question.

A general solution for such as situations is to add error handling[^] via adding TRY... CATCH[^] block (instruction). This might help you identify where the error occurs.

For further information, please see:
Using TRY...CATCH in Transact-SQL[^]
Error Handling in Data[^]
 
Share this answer
 
SET XACT_ABORT ON

When SET XACT_ABORT is ON, if a Transact-SQL statement raises a run-time error, the entire transaction is terminated and rolled back.

https://msdn.microsoft.com/en-us/library/ms188792.aspx for ref
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900