error handling in database in asp.net

  • Thread starter Thread starter basha
  • Start date Start date
B

basha

hi

can u help me how to write code to handle errors in
dabase in asp.net pls,very urgent

bye
basha
 
Hi,

All database errors result in some form of exception, so wrap all your
database access calls in a try catch block.
Then catch SqlException or OleDbException(depending on the provider you are
using) and perform whatever cleanup/error reporting action you need.

E.g.

try
{
// do some database action here
}
catch(SqlException ex)
{
// log error to event log or display error message to user.
}


Another tip if you are using C# is to use the "using" statement.

using(SqlConnection con = new SqlConnection(connectionstring))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ......",con);
cmd.ExecuteScalar();
}


If something goes wrong in the database access code, the "using" statement
will make sure
that the Dispose method will be called on the SqlConnection object, that way
the connection to your
database becomes closed. Making sure that you close your database
connections is very important,
they are a limited resource and if you forget to close a lot of them you
will notice it, especially in a web application.


Hope this helps,

Chris
 
Back
Top