Make Mysql errors caught in c# user friendly

17,964

Solution 1

As Marc B pointed that every Mysql error has an error code, so you can catch a MySqlException using a try ... catch block like this:

try
{

}
catch (MySqlException ex)
{
    int errorcode = ex.Number;
}

So you can write a case statement to provide the error message for each error code, here are a list of server error codes and messages.

Solution 2

You could try catching the specific sql error message and display it

Try

Catch ex as SqlException
'''sql specific error message
''ie: 
response.write("oops! error message: " & ex.message)
Catch ex as Exception
'''any other runtime error messages
End Try
Share:
17,964
Stainedart
Author by

Stainedart

Software engineer working with search engines, text mining well anything related to content!

Updated on June 22, 2022

Comments

  • Stainedart
    Stainedart almost 2 years

    I am trying to return user friendly error message when a Mysql Exception is thrown in c#. I am currently returning the exception message but that message is not very user friendly. So I was wondering if any one of you had any trick that does not require any fancy regex parsing of the error messages received to display them to the user in a manner that would make sense to them.

    I am trying to stay away from complex validation code prior to inserting/updating/deleting a record but that seems to be the only way ... unless you know better!