• Home
  • About
  • BestBloggingIdeas
  • DotNetLearningSource
  • FORUM
  • Joblinks
  • Latest News
  • Policy
  • POSTS
  • SimplySqlServer.Com && SimplyAspDotNet.Com
  • Sitemap

Join Ours Forum

Asp.Net,C#,Ajax,Sql server,silverlight,Javascript codes exambles articles,Programming exambles

RSS Feed
  • Bounty Huge Roll [Amazon Frustration-Free Packaging]
  • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
  • The Basic Overview of Windows Mobile Development Asp.Net C#
  • Overview of Sql server extended properties Asp.Net C#
  • How to Use Sql Server Extended properties using visual studio Asp.Net C#
  • Adobe Dreamweaver Templates Accelerate Web Development
  • Top Tips for Web Design Projects
  • How to Achieve a Good Web Design Structure
  • To Use Or Not To Use Website Templates
  • Five Tips to a Successful Website
  • Top 10 Articles,


    Silverlight Datagrid Select Update Delete Insert Asp.Net C#

    Differences Similarities Benefits Between Typed Datasets and Untyped Datasets asp.net c#

    Linq to Sql Introduction Entities Ado.Net C# SqlClasses Attributes Linq Mapping

    Linq Programming/How Linq Works?/Linq Implementation In Asp.Net C# Ado.Net

    Performing Developing Using Investigating Asp.Net 2.0 Ajax Application Development Asp.Net C#

    Hosting/Install Wcf Services in a Windows Service Asp.Net C#

    Connecting Silverlight to Wcf Asp.Net C#

    Silverlight Data Grid Data Binding WCF Asp.Net C#

    Invoking/Accessing/Calling WCF Service Without Adding/Creating Proxy/Reference Asp.Net C#

    Performing Doing Creating Insert Update Delete sql data Using Linq Database Asp.Net C#

    Exception Handling in TRANSACTIONS in SQL Server 2008

    Posted by james on April 7, 2010 Leave a comment (10) Go to comments

    Introduction:
    In this article,i am going to explain about how to handle the exception in transactions using sql server 2008.

    Main:

    SQL Server offers major improvements in error handling inside T-SQL transactions. As of SQL Server 2005, you can catch T-SQL and transaction abort errors using the TRY/CATCH model without any loss of the transaction context. The only types of errors that the TRY/CATCH construct can’t handle are those that cause the termination of your session (usually errors with severity 21 and above, such as hardware errors). The syntax is shown here:

    BEGIN TRY
      --sql statements
    END TRY
    BEGIN CATCH
      --sql statements for catching your errors
    END CATCH

    If an error within an explicit transaction occurs inside a TRY block, control is passed to the CATCH block that immediately follows. If no error occurs, the CATCH block is completely skipped.

    You can investigate the type of error that was raised and react accordingly. To do so, you can use the ERROR_xxx functions to return error information in the CATCH block, as shown in below,

    T-SQL exception handling example

    BEGIN TRY
      SELECT 5/0
    END TRY
    BEGIN CATCH
      SELECT
       ERROR_NUMBER()    AS ErrNumber,
       ERROR_SEVERITY()  AS ErrSeverity,
       ERROR_STATE()     AS ErrState,
       ERROR_PROCEDURE() AS ErrProc,
       ERROR_LINE()      AS ErrLine,
       ERROR_MESSAGE()   AS ErrMessage
    END CATCH

    You can examine the value reported by any of the various ERROR_xxx functions to decide what to do with the control flow of your procedure and whether to abort any transactions. In our example in below examble, which attempts to divide by zero, here are the values returned by the error functions. (The ERROR_PROCEDURE function returns NULL in this example because the exception did not occur within a stored procedure.)

    ErrNumber ErrSeverity ErrState ErrProc ErrLine ErrMessage
    ———- ———— ———- ——– ——– ———————————-
    8134 16 1 NULL 2 Divide by zero error encountered.

    When you experience a transaction abort error inside a transaction located in the TRY block, control is passed to the CATCH block. The transaction then enters a failed state in which locks are not released and persisted work is not reversed until you explicitly issue a ROLLBACK statement. You’re not allowed to initiate any activity that requires opening an implicit or explicit transaction until you issue a ROLLBACK.

    Certain types of errors are not detected by the TRY/CATCH block, and you end up with an unhandled exception even though the error occurred inside your TRY block. If this happens, the CATCH block is not executed. This is because CATCH blocks are invoked by errors that take place in actual executing code, not by compile or syntax errors. Two examples of such errors are syntax errors and statement-level recompile errors (for example, selecting from a nonexistent table). These errors are not caught at the same execution level as the TRY block, but at the lower level of execution—when you execute dynamic SQL or when you call a stored procedure from the TRY block. For example, if you have a syntax error inside a TRY block, you get a compile error and your CATCH block will not run, as shown here:

    -- Syntax error doesn't get caught
    BEGIN TRY
       SELECT * * FROM Customer
    END TRY
    BEGIN CATCH
       PRINT 'Error'
    END CATCH

    The result is an error from SQL Server, not from your CATCH block, as follows:

    Msg 102, Level 15, State 1, Line 2
    Incorrect syntax near ‘*’.

    Statement-level recompilation errors also don’t get caught by CATCH blocks. For example, using a nonexistent object in a SELECT statement in the TRY block forces an error from SQL Server, but your CATCH block will not execute, as shown here:

    -- Statement level recompilation doesn't get caught
    BEGIN TRY
      SELECT * FROM NonExistentTable
    END TRY
    BEGIN CATCH
      PRINT 'Error'
    END CATCH

    The result is an error from SQL Server, as follows:

    Msg 208, Level 16, State 1, Line 3
    Invalid object name ‘NonExistentTable’.

    When you use dynamic SQL or a stored procedure, these types of compile errors do get caught because they are part of the current level of execution. Each of the SQL blocks shown in below examble will execute the CATCH block.

    Catching syntax and recompilation errors in dynamic SQL and stored procedure calls with exception handlers

    -- Dynamic SQL Example
    BEGIN TRY
      EXEC sp_executesql 'SELECT * * FROM Customer'
    END TRY
    BEGIN CATCH
      PRINT 'Error'
    END CATCH
    GO
     
    -- Stored Procedure Example
    CREATE PROCEDURE MyErrorProc
    AS
      SELECT * FROM NonExistentTable
    GO
     
    BEGIN TRY
      EXEC MyErrorProc
    END TRY
    BEGIN CATCH
      PRINT 'Error'
    END CATCH

    Conclusion:
    Hope this helps,
    Happy Coding.

    SQL SERVER/ORACLE
    ← How to Create/Develop/Add Ranking Functions in SQL Server 2008
    How To Use WAITFOR Statement in sql server 2008 →

    Learn Easily Using Video Tutorials


    How to choose the right Java IDE – explained Eclipse NetBeans BlueJ

    Developing/Creating/Performing/Configuring Java Applications Using Eclipse IDE

    Step By Step Guide for Download/Install Configure Eclipse IDE for Java

    Editing data with the GridView control Asp.Net C#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Best way to prepare asp.net Interview - Success Stories

    Download Important Questions and PPT's:

    Sql Server Important Questions Online free download

    Dotnet Important Questions Online free download

    Exploring Linq to Sql Process Flow

    Learn how to perform silverlight programming

    Learn OOPs concepts in better and well manner

    Learn Ajax in better and well manner

    Leave a comment

    10 Comments.

    1. forex robot May 12, 2010 at 8:13 am

      found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later

    2. veterinary technician June 5, 2010 at 4:45 am

      Keep posting stuff like this i really like it

    3. foam mattress June 8, 2010 at 6:09 pm

      I really enjoyed reading your post here and I just wanted to tell you that I totally agree with what you’re saying! It’s hard to find people that think alike these days. Keep it up

    4. Wordpress Themes June 10, 2010 at 1:33 pm

      Amiable fill someone in on and this mail helped me alot in my college assignement. Say thank you you as your information.

    5. Ethan Reid June 26, 2010 at 3:15 am

      Sound good. Its also my favorite topic.That’s great andthanks for the fine sharring.

    6. Raelene Meinhardt June 28, 2010 at 8:48 am

      Nice job!

    7. sell junk car June 30, 2010 at 8:24 am

      Great post! I started following your blog about a month ago and I like your honesty. Good example to emulate.

    8. Odell Amavisca August 12, 2010 at 3:19 am

      thank you for posting this one up..it is very interesting

    9. pc support August 12, 2010 at 3:22 pm

      I just needed to say that I found your blog via Goolge and I am glad I did. Keep up the good work and I will make sure to bookmark you for when I have more free time away from the books. Thanks again!

    10. Aubrey Laplume September 14, 2010 at 4:16 am

      Hello dude,i like this Ones New blog ideal much. achieve u allow suggestion for my homepage? thanks being Your attention

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    *

    *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Enter your email address:

    Delivered by FeedBurner

    • Recent Posts

      • Bounty Huge Roll [Amazon Frustration-Free Packaging]
      • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
      • The Basic Overview of Windows Mobile Development Asp.Net C#
      • Overview of Sql server extended properties Asp.Net C#
      • How to Use Sql Server Extended properties using visual studio Asp.Net C#
    • Search by Tags!

      Application AspNet Basic between Black Bluetooth Build Business Collection Consultants Design Development Downloading effective Excel Experts Generics Implement Installing Interview Logic Management Microsoft Minutes Object Outlook Professional Programmer Programming Project Projects Questions Ready Select Server Services Silverlight Source Strings Studio Through using Visual Website Wordpress
    • Archives

      • August 2011
      • June 2011
      • May 2011
      • April 2011
      • March 2011
      • February 2011
      • December 2010
      • November 2010
      • October 2010
      • September 2010
      • August 2010
      • July 2010
      • June 2010
      • May 2010
      • April 2010
      • March 2010
      • February 2010
      • January 2010
      • December 2009
      • November 2009
      • October 2009
      • September 2009

    Copyright © 2012 NetProgrammingHelp.com

    Δ Top