error handling in database in asp.net

B

basha

hi

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

bye
basha
 
C

Christopher Kimbell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top