i have this procedure for inserting rows in tables(sql server 2005)
CREATE PROCEDURE ans_insert
(
@q_desc varchar(2000),
@sub_id int,
@marks int,
@ans1 varchar(1000),
@ans varchar(1000),
@userid varchar(15),
@cr_date datetime
)
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
DECLARE @q_id int
insert into questions(q_desc,sub_id,marks,created_by,DT_created) values(@q_desc,@sub_id,@marks,@userid,@cr_date);
SET @q_id = IDENT_CURRENT('questions')
INSERT INTO answers(ans_desc,q_id,created_by,DT_created,istrue)
VALUES( @ans1,@q_id,@userid,@cr_date,
CASE WHEN @ans1 =@ans THEN 1 ELSE 0 END);
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
DECLARE @ErrorLine INT;
SELECT @ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorLine=ERROR_LINE(),
@ErrorState = ERROR_STATE();
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
RAISERROR (@ErrorMessage,@ErrorSeverity,@ErrorState,@ErrorLine);
END CATCH
END
ans i call this from my ASP.NET form as
AnsCmd is my stored procedure command...after adding all params
try
{
conn.Open();
AnsCmd.ExecuteNonQuery();
lblMsg.Visible = true;
lblMsg.Text = "success";
conn.Close();
}
catch (SqlException sqlex)
{
lblMsg.Visible = true;
lblMsg.Text = sqlex.ToString();
}
catch (Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = ex.ToString();
}
to check that raiserror is working,i changed the table name in insert from answers to answers1 which does not exist..
when executed i get error message as
System.Data.SqlClient.SqlException: Invalid object name 'answers1'. Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 0, current count = 1. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,......
so is this thing working properly or m i missing something???