I have a small question in my mind.
Suppose I make a big stored procedure in sql server and call this sp from c# application using ado.net.
if sp returns any error or exception now i want to get the actual error with error code in application end.
Please suggest how to do ??
Thanks in advance.
Loading


Khan Abrar AhmedRank #778 · 3K reputationPosted Jul 4, 2014, 2:37 AM EDT
BEGIN TRY
BEGIN TRANSACTION
--Place the Insert update logice
---
COMMIT TRANSACTION
END TRY
BEGIN CATCH
--Roll back the transaction
ROLLBACK TRANSACTION
--If any error the raise the error
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage,
@ErrorSeverity,
@ErrorState
);
END CATCH
Anupam SinghRank #643 · 3.7K reputationPosted Jul 4, 2014, 2:45 AM EDT
Actually its no need to write any additional code, SqlException class will handle it for you.
Ravi ShekharRank #673 · 3.6K reputationPosted Jul 4, 2014, 3:17 AM EDT
Ravi ShekharRank #673 · 3.6K reputationPosted Jul 4, 2014, 3:24 AM EDT
There are multiple types of error and exception we caught in our application.
Is there any built in function in Visual studio to get a proper msg for that error or exceptions which user understand easily.
I can make this type of function but no. of errors and exceptions are very huge.
Then how ??
Ravi
Anupam SinghRank #643 · 3.7K reputationPosted Jul 4, 2014, 8:56 PM EDT
You can get error codes in SYS.MESSAGES system view (in master db).
and catch it using
try
Munesh SharmaRank #174 · 13.9K reputationPosted Jul 4, 2014, 10:44 PM EDT