catch mysql-exception using .Number


I want to catch a specific mysql error with the number 1046. If i use MessageBox.Show to display the error number it is always 0, no matter which Exception it is. Shouldn't the Exception have a number like the number 1046?

catch (MySqlException mysqlex) { MessageBox.Show(mysqlex.Number.ToString()); }

EDIT: Sorry, i am using the MySQL-Connector / Net.


You don't see a number because you did not connect to a database. And probably MySQL is assigning numbers to error exceptions.

If you look at description for Number property <a href="http://dev.mysql.com/doc/connector-net/en/connector-net-ref-mysqlclient-mysqlexceptionmembers.html" rel="nofollow">here</a> it says


Gets a number that identifies the type of error. This number corresponds to the error numbers given in <strong>Server</strong> Error Codes and Messages.


Because you did not made a connection to the server you cannot get server error code.

Check your connection string.


Probably same problem as <a href="https://stackoverflow.com/questions/24899020/c-sharp-mysql-connector-number-always-0" rel="nofollow">this</a>.

please try in the following code.



  • Updating datagridview with mysql
  • mysql load data local show warning from C#
  • Entity Framework : Set MySQL custom environment variables
  • The 'await' operator can only be used within an async method [MySQL connector]
  • Uploading entity with parent using bulkloader
  • Issue in gzip compression
  • Can't connect to MySQL for .NET application deployed in Wine using Ubuntu
  • Are Richfaces and Primefaces compatible with each other?
  • Switch to popup in python using selenium
  • Redirect to Post Method/Action
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • Dynamically switching connect in Modelica
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Adding elements to a huge XML file
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Redux Form - Not able to type anything in input
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Blackberry - Custom EditField Cursor
  • Android full screen on only one activity?
  • Django: Count of Group Elements
  • Is there a javascript serializer for JSON.Net?
  • Fetching methods from BroadcastReceiver to update UI
  • Get object from AWS S3 as a stream
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Cross-Platform Protobuf Serialization
  • javascript inside java/jsp code
  • Validaiting emails with Net.Mail MailAddress
  • Where to put my custom functions in Wordpress?
  • Javascript convert timezone issue
  • Symfony2: How to get request parameter
  • Do I've to free mysql result after storing it?
  • GridView Sorting works once only
  • Android Studio and gradle
  • Buffer size for converting unsigned long to string
  • WPF Applying a trigger on binding failure
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?