Database Error Handling In C#
Contents |
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students
C# Database Exception Handling
Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events error handling in c# best practices Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired error handling c# mvc content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. .NET Framework Class Library
Global.asax Error Handling C#
System.Data Namespaces System.Data.SqlClient System.Data.SqlClient SqlException Class SqlException Class SqlException Class ApplicationIntent Enumeration OnChangeEventHandler Delegate PoolBlockingPeriod Enumeration SortOrder Enumeration SqlAuthenticationMethod Enumeration SqlBulkCopy Class SqlBulkCopyColumnMapping Class SqlBulkCopyColumnMappingCollection Class SqlBulkCopyOptions Enumeration SqlClientFactory Class SqlClientMetaDataCollectionNames Class SqlClientPermission Class SqlClientPermissionAttribute Class SqlColumnEncryptionCertificateStoreProvider Class SqlColumnEncryptionCngProvider Class SqlColumnEncryptionCspProvider Class SqlColumnEncryptionKeyStoreProvider
Sqlexception C#
Class SqlCommand Class SqlCommandBuilder Class SqlCommandColumnEncryptionSetting Enumeration SqlConnection Class SqlConnectionColumnEncryptionSetting Enumeration SqlConnectionStringBuilder Class SqlCredential Class SqlDataAdapter Class SqlDataReader Class SQLDebugging Class SqlDependency Class SqlError Class SqlErrorCollection Class SqlException Class SqlException Methods SqlException Properties SqlInfoMessageEventArgs Class SqlInfoMessageEventHandler Delegate SqlNotificationEventArgs Class SqlNotificationInfo Enumeration SqlNotificationSource Enumeration SqlNotificationType Enumeration SqlParameter Class SqlParameterCollection Class SqlProviderServices Class SqlRowsCopiedEventArgs Class SqlRowsCopiedEventHandler Delegate SqlRowUpdatedEventArgs Class SqlRowUpdatedEventHandler Delegate SqlRowUpdatingEventArgs Class SqlRowUpdatingEventHandler Delegate SqlTransaction Class TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. SqlException Class .NET Framework (current version) Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 .NET Framework 3.5 .NET Framework 3.0 .NET Framework 2.0 .NET Framework 1.1 The exception that is thrown
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this c# sqlexception error codes site About Us Learn more about Stack Overflow the company Business Learn more
C# Catch Database Connection Exception
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss dbexception c# Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up how to handle https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(v=vs.110).aspx db exception in c# up vote 5 down vote favorite 1 my question is how to handle sql exception in c#, is there anyway to check what kind of the sql exception throws from data access layer? For example, if db throws an unique constraint exception, or foreign key exception, is there any way to catch it from c#? what's the exception handling pattern http://stackoverflow.com/questions/1666887/how-to-handle-db-exception-in-c-sharp you are using for these db exception? c# exception share|improve this question asked Nov 3 '09 at 11:56 user192415 1731211 add a comment| 4 Answers 4 active oldest votes up vote 5 down vote Have a look at the documentation of the SQLException class, in particular, at its properties: SQLException.Number, for example, should allow you to identify which type of SQLException (unique constraint, foreign key, ...) occurred. In VB.NET, you could use conditional catch: Try ... Catch ex as SqlException When ex.Number = ... ... Catch ex as SqlException When ex.Number = ... ... End Try In C#, you will have to resort to try { ... } catch (SqlException ex) { if (ex.Number == ...) { ... } else if (ex.Number == ...) { ... } else { throw; // re-throw exception instead of handling it } } share|improve this answer answered Nov 3 '09 at 12:07 Heinzi 96.9k27206319 add a comment| Did you find this question interesting? Try our newsletter Sign up for our newsletter and get our top new questions delivered to your inbox (see an example). Subscribed! Success! Please click the link in the confirmation email to a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and http://stackoverflow.com/questions/15559508/catching-database-exceptions policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags http://csharp-station.com/Tutorial/CSharp/Lesson15 Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only error handling takes a minute: Sign up Catching Database Exceptions up vote 0 down vote favorite So I'm designing a Web Service and of course; I need this Service to automatically write to a Database. Which is quite straight forward; but obviously Sql has a tendency to not play nice. Which pretty much creates a nightmare when troubleshooting, not to mention through a error handling in Web Service would make it even more of a nightmare to debug. So I checked the internet and stumbled across an article on Stack Overflow that talked about a SqlHelper Class that essentially had a massive list of these: public static bool IsDuplicateId(SqlException sex) { return (sex.Number == 2601); } So that implementation would be tedious, as you'd have to call all of these Methods. However, someone answered with: switch (e.Number) case 2601: // Do Something break; default: throw; So I thought why not create a Class to handle a majority of these possible errors. With this particular implementation in mind: public class SqlExceptionHelper { public SqlExceptionHelper(SqlException sqlException) { // Do Nothing. } public static string GetSqlDescription(SqlException sqlException) { switch (sqlException.Number) { case 21: return "Fatal Error Occurred: Error Code 21."; case 53: return "Error in Establishing a Database Connection: 53."; default return ("Unexpected Error: " + sqlException.Message.ToString()); } } } So my thought process is I have a Class that can be reused to detect some common errors; and in other classes I simply using SomeNamespace.ExceptionHelpers; I could implement somethi
Disconnected Data – The DataSet and SqlDataAdapter Lesson 06: Adding Parameters to Commands Lesson 07: Using Stored Procedures C# Tutorial Lesson 1: Getting Started with C# Lesson 2: Operators, Types, and Variables Lesson 3: Control Statements – Selection Lesson 4: Control Statements – Loops Lesson 5: Methods Lesson 6: Namespaces Lesson 7: Introduction to Classes Lesson 8: Class Inheritance Lesson 9: Polymorphism Lesson 10: Properties Lesson 11: Indexers Lesson 12: Structs Lesson 13: Interfaces Lesson 14: Introduction to Delegates and Events Lesson 15: Introduction to Exception Handling Lesson 16: Using Attributes Lesson 17: Enums Lesson 18: Overloading Operators Lesson 19: Encapsulation Lesson 20: Introduction to Generic Collections Lesson 21: Anonymous Methods Lesson 22: Topics on C# Type Lesson 23: Working with Nullable Types LINQ Tutorial Lesson 01: Introduction to LINQ Lesson 02: Forming Projections Articles Links ASP.NET Sites C# Sites .NET Sites Job Sites Object Oriented Sites Other Sites Tools VB.NET Sites Web Services Sites About Contact Link to C# Station Submit a site Support C# Station Terms of Service Lesson 15: Introduction to Exception Handling This lesson teaches how to handle exceptions in your C# programs. Our objectives are as follows: Learn what an exception is Implement a routine with a try/catch block Release resources in a finally block Exceptions Exceptions are unforeseen errors that happen in your programs. Most of the time, you can, and should, detect and handle program errors in your code. For example, validating user input, checking for null objects, and verifying the values returned from methods are what you expect, are all examples of good standard error handling that you should be doing all the time. However, there are times when you don't know if an error will occur. For example, you can't predict when you'll receive a file I/O error, run out of system memory, or encounter a database error. These things are generally u