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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With