Er. alokpandey's Blog

Why should we use delegates?

Posted in Uncategorized by Alok Kumar Pandey on February 25, 2011

“Delegates are like function pointers of C++. They reference methods that can be executed using delegates. A method can also be called using class object. So what is the advantage of using delegates? What can a delegate do that a normal function call cannot?”

Major Advantages:

i. Encapsulating the method’s call from caller.
ii. Effective use of Delegat improves the performance of application.
iii. Used to call a method asynchronously.
iv. Can be used in Multi Threaded application.

In a place where threads are being used and there is no direct way to call the methods inside a thread in that case we may make use of a delegated because using that we can call any methods inside a running thread too.

Tagged with: ,

How to handle SQL error in Stored Procedure

Posted in Uncategorized by Alok Kumar Pandey on September 21, 2010

When writing SQL procedures, it is really important to handle errors cautiously. Having that in mind will probably save your efforts, time and money. I have been working with MS-SQL 2000, MS-SQL 2005, MS-SQL 2008 (I have not got the opportunity to work with MS-SQL 2008 R2 yet) for many years now and I want to share with you how I handle errors in T-SQL Stored Procedure. This code has been working for many years now without a hitch.

BEGIN TRANSACTION;
BEGIN TRY
— Code in transaction go here COMMIT TRANSACTION;
Declare @myVal int
Set @myVal = ‘dfgdfd’
Select @myVal
END TRY
BEGIN CATCH
— Rollback on error
ROLLBACK TRANSACTION;
— Raise the error with the appropriate message and error severity
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int;
SELECT @ErrMsg = ERROR_MESSAGE(), @ErrSeverity = ERROR_SEVERITY();
Select @ErrMsg as ErrMessage,@ErrSeverity as ErrSeverity
–RAISERROR(@ErrMsg, @ErrSeverity, 1);

END CATCH;