By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

error handling in database in asp.net

P: n/a
hi

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

bye
basha
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"basha" <ba*******@yahoo.com> wrote in message
news:0b****************************@phx.gbl...
hi

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

bye
basha

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.