Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Mysql errors caught in c# user friendly

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!

like image 474
Stainedart Avatar asked Nov 15 '11 15:11

Stainedart


2 Answers

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.

like image 54
Mahmoud Gamal Avatar answered Sep 20 '22 01:09

Mahmoud Gamal


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
like image 26
Losbear Avatar answered Sep 22 '22 01:09

Losbear