Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I'm razing custom errors in sql, like this

RAISERROR( 'Invalid id', 16, 1 )
RETURN


I want to show this(custom raised) error to user but not any other sql exeptions.

for example

try
{
}
catch(SqlException ex)
{
  if (customRaisedError)  //how to know ?
  {
       //show
  }
  else
  {
      //dont show
  }
}
catch(Exception ex)
{
//show all these error
}



I can use prefix in message, but I do not like this way. Any other perfect way you guys know ?
Posted

 
Share this answer
 
Yes, whenever you get to raise this custom error in SQL, you can return some integer value like

SQL
RAISERROR( 'Invalid id', 16, 1 )
SELECT '1';
RETURN



This way you can get a separate value for any raised error at the front-end while executing the stored procedure, and you can display the message accordingly on your front end, and then put a blank as

C#
return "";


in other catch blocks, to avoid showing those exceptions to users.
Hope this helps.
 
Share this answer
 
Comments
Xmen Real 29-Nov-10 6:22am    
AFAIK if there is an error in sql statement, you cant get value.
Hello XMen

You can create a custom Exception Handler.

Create a lookup table in sql for the error code / description / language columns and enabled.

if the enabled is bit 0 (off) you can throw the default exception in your code area.

Otherwise throw the exception that related to the table entry.

i advise you populate the content of the lookup in your app as part of the initialization starting process. then easier to throw it from a error class or component / manager
C#
int y = 0;

   try
   {
      y = 2* 8888888888888888888888888888888 ^ 999999999999999999999
   }
   catch (Exception _Ex)
   {
      DisplayCustomErrror(_Ex);
   }
   finally
   {
     y = 2;
   }


public void DisplayCustomErrror(Exception _Ex)
{
   //  Check if the code error exists in your list of errors.
   // if not amend to it to the table as a new entry with a default of  the standard exception message - you can get change it after wards when the app is distributed.


    // Throw Custom Exception


   // If custom Exception doesn't exist - throw default

}
 
Share this answer
 
v2
Comments
Xmen Real 28-Nov-10 23:29pm    
I read your answer more than 3 times, but I have no idea what you trying to say. Maybe you got me wrong.
Sandeep Mewara 29-Nov-10 1:16am    
Use PRE tags to format your code part. It makes the answer readable.
fjdiewornncalwe 29-Nov-10 7:41am    
X, Please, please, please read the whole question BEFORE you answer the question. You could be one of the most brilliant people around, but if you don't read the question completely, your answer will always come across half-baked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900