Code for Catch SQL Exception used in ADO.Net


In this i decribe about the Code used in ADO.Net for catching Sql Exception

This is a code for sql exception which automatically redirect to the error page if any sql error occured in ADO.Net.


catch (SqlException exp)
{
StreamWriter wr = new StreamWriter(@"D:\errorlog.log");
string a = exp.Source + " " + exp.LineNumber + " " + exp.Message;

wr.WriteLine(a);
wr.Close();
Response.Redirect("page_error.aspx");
}


Reference: http://forums.asp.net/t/555900.aspx


Related Articles

Try... Catch in SQL

Try Catch in SQL is the one of the best option to be used with the transaction. We can begin the Transaction immediate after the Begin try and at the end the try we can commit the transaction. It means all the Queries inside the Try executed successfully. And inside the Catch, we can rollback the Transaction since the catch will be executed only if there as there some exception occurs and all or some of the queries inside the try is not executed.

More articles: ADO.Net exception handling SQL exception Exception handling

Comments

Author: srirama13 Apr 2013 Member Level: Gold   Points : 0

hi,

You can print the date and time also in the error log file.So that the user can now when the error has occured and can you get the line code number in asp.net?..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: