Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This is dinakar. Please tell me how to handle exceptions of sql server using its error number while we are connecting to sqlserver using c#.net code...please help me out
Posted

You can catch for SqlExceptions and check the numbers

The numbers are defined here
http://msdn.microsoft.com/en-us/library/aa937595%28v=sql.80%29.aspx[^]

C#
try
{
    // Code to access DB
}
catch (SqlException ex)
{
    switch (ex.Number)
    {
        case 2764:
            // do something when error 'Incorrect password supplied for application role' is thrown
            break;
    }
}
 
Share this answer
 
Comments
Member 7962316 24-Jun-11 5:49am    
thanks you so much
thatraja 24-Jun-11 6:18am    
BTW you can vote his answer.
thatraja 24-Jun-11 6:18am    
Spot on, 5!
If SQL Server throws an exception, you can catch it, and examine the exception for the error number. If a number occurs within a stored proc and you catch it in SQL Server, you can return the error number. I'm not sure what else you are trying to ask ?
 
Share this answer
 
Comments
Member 7962316 24-Jun-11 5:41am    
please give me example in c#.net for catching exception by using error number of sql server . for example i am opening sqlconnection as
try
{
sqlconnection con=new sqlconnection();
con.open();
------
con.close()
}

catch()
{

}
please tell me how to handle exception with error number for the above code
Put a try...catch block around teh code which is accessing it, and examine the exception in the catch block. There is no specific part of an exception that is the error number: you will have to work from the exception type and message.
try
   {
   // Code to access DB
   }
catch (Exception ex)
   {
   ///Handle error here
   }
 
Share this answer
 
Comments
Member 7962316 24-Jun-11 5:48am    
i mean give example with error number and handling it

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