how to catch sql exception with its error code(error no) to identify which exception is exactly thrown in c# ?
for example database is offline or there is reference row in other table so sql wont allow me to delete row but how exactly i could identify what is the problem in catch so that i can display message to the user in my application
how to catch sql exception with its error code(error no) to identify which exception
Share
You need to put your database code into a
try ... catchblock, and (assuming you’re using ADO.NET against SQL Server) then catch theSqlException.The
SqlExceptionobject will contain a collection ofSqlErrorobjects that describe the error that happened in great detail – with line number, error code and everything.Other databases will have similar constructs – check your docs!