After confirming an order, if the user refreshes the page, the app will try to populate the sameSession.SessionID in theCustDetails table but since the columnOrderID is aPrimary Key column in the tableCustDetails, it won't accept duplicate OrderIDs. Under such circumstances, anOleDbException will be raised.
Since a DB app can throw otherOleDbExceptions other than the one about which I mentioned above, I want to display custom error messages to the user. For e.g. if he refreshes the page after confirming his order, I want to display a message saying "Your order has already been placed".
To do this, I tried using theErrorCode property of theOleDbException class but what I found is theErrorCode changes from time to time! Had a particularErrorCode been assigned to the error, I could have done something like this (assuming that theErrorCode for the above error is-12345 which is constant):
Try
'some code
Catch ex As OleDbException
If (ex.ErrorCode = -12345) Then
Response.Write("Your order has already been placed")
ElseIf (ex.ErrorCode = <some other constant ErrorCode>) Then
Response.Write("Another custom error message")
End If
End Try
But I can't do the above since theErrorCode changes from time to time.
So how do I display custom error messages to users under such circumstances?
Of course, I can use theMessage property of theOleDbException class but that would be a rather tedious workaround.
I would provide user friendly messages for the the common errors in a shared function.
There are too many error codes to redefine them all. Putting the code in a central function allows you to call it from anywhere (excuse my C# but you get the idea)
String ExceptionToFriendlyString(OldDbExcption Ex){switch(Ex.ErrorCode) {case 1234:return"That record has already been inserted";case 5678:return"Friendly error message";default:return Ex.Message; // default to Exception Message }}|||Thanks for your suggestion, Steve, but as already pointed in post #1 in this thread, the ErrorCode goes on changing. For e.g. when I try to insert a record in a DB table that already exists in that DB table, the ErrorCode turns out to be, say, 1234.
Next I shut down my machine & restart it. Now when I try to insert a record in the same DB table which already exists in the table, then the ErrorCode changes to, say, 5678. Of course, I can use a default message as you have shown in your code but (again) as already pointed out, I want the error messages to be as precise as possible.
Any other suggestions?|||
I think you will find that the reason you are getting different error codes is because you are getting different errors.
|||
Hi RN5A,
I agree that the error code stays fixed when you get certain kind of error. When error code changes, the type of error gets changed.
No comments:
Post a Comment