Database Error Handling C#
Contents |
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs error handling in c# best practices and reference Dev centers Retired content Samples We’re sorry. The content you
Error Handling C# Mvc
requested has been removed. You’ll be auto redirected in 1 second. MFC Desktop Applications Concepts Exception Handling Exception global.asax error handling c# Handling Exceptions: Database Exceptions Exceptions: Database Exceptions Exceptions: Database Exceptions Exceptions: Changes to Exception Macros in Version 3.0 Exceptions: Catching and Deleting Exceptions Exceptions: Converting from MFC Exception Macros Exceptions:
Exception And Error Handling In C#
Using MFC Macros and C++ Exceptions Exceptions: Examining Exception Contents Exceptions: Freeing Objects in Exceptions Exceptions: Throwing Exceptions from Your Own Functions Exceptions: Exceptions in Constructors Exceptions: Database Exceptions Exceptions: OLE Exceptions 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 c# error handling in constructor maintained. Exceptions: Database Exceptions Visual Studio 2015 Other Versions Visual Studio 2013 Visual Studio 2012 Visual Studio 2010 Visual Studio 2008 Visual Studio 2005 Visual Studio .NET 2003 This article explains how to handle database exceptions. Most of the material in this article applies whether you are working with the MFC classes for Open Database Connectivity (ODBC) or the MFC classes for Data Access Objects (DAO). Material specific to one or the other model is explicitly marked. Topics include: Approaches to exception handlingA database exception-handling exampleApproaches to Exception HandlingThe approach is the same whether you are working with DAO or ODBC.You should always write exception handlers to handle exceptional conditions. The most pragmatic approach to catching database exceptions is to test your application with exception scenarios. Determine the likely exceptions that might occur for an operation in your code, and force the exception to occur. Then examine the trace output to see what exception is thrown, or examine the returned error information in the debugger. This lets you know which return codes you'll see for the exception scenarios
resources Windows Server 2012 resources Programs MSDN subscriptions
C# Error Handling Get Line Number
Overview Benefits Administrators Students Microsoft Imagine Microsoft
C# Error Handling Framework
Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 c# error handling techniques Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll https://msdn.microsoft.com/en-us/library/19b8k939.aspx be auto redirected in 1 second. .NET Framework Class Library 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 https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(v=vs.110).aspx Class SqlClientMetaDataCollectionNames Class SqlClientPermission Class SqlClientPermissionAttribute Class SqlColumnEncryptionCertificateStoreProvider Class SqlColumnEncryptionCngProvider Class SqlColumnEncryptionCspProvider Class SqlColumnEncryptionKeyStoreProvider 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) Othe
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 site About http://stackoverflow.com/questions/1666887/how-to-handle-db-exception-in-c-sharp Us Learn more about Stack Overflow the company Business Learn more about hiring http://stackoverflow.com/questions/15559508/catching-database-exceptions developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags 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 takes a minute: Sign up how to handle db exception in error handling 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 you are using for these db c# error handling 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| up vote 1 down vote you can check message text,Number and do switch case on it to know the error... try { } catch (SqlException ex) { string str; str = "Source:"+ ex.Source; str += "\n"+ "Number:"+ ex.Number.ToString(); str += "\n"+ "Message:"+ ex.Message; str += "\n"+ "Class:"+ ex.Class.ToString (); str
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 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 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 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 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: " + sqlExce