catch sql exception

G

gerry

My 1st crack at doing things the RAD way - I have a web form with a
SqlDataSource and a DetailsView.
I am trying to find a way to catch database exceptions so that i can display
an appropriate error message and let the user fix the data and resubmit the
form. ( In this case it is a dup key violation - so nothing I can pre-edit
for )

The only way I can see to catch db exceptions is by overriding Page.OnError
and looking at Context.Error. This gives me access to the exception and
Context.ClearError() stops the default error handling but by this point the
response has been cleared and so returns an empty page to the browser.
I am surprised that there isn't an OnError associated with either the
SqlDataSource or the DetailsView.

It almost looks like I am going to be forced to scrap the SqlDataSource and
go back to manual databinding etc in order to be able to catch db
exceptions.

I haven't found any references to this in google - so I assume that I am
missing something obvious.

Gerry
 
G

geekboy0001

Wrap the SQL call within a TRY-CATCH. ie:
Try
DataConnection.Open
DataReader.Open -- some sql call
DataReader.Read or whatever
The sql call will fail here
Catch ex as exception
Do your error handling here. your error is ex.message
Make sure to close your reader and your connection here too
End Try
 
G

gerry

Hey geek ,

well that is exactly the problem., when using an SqlDataSource none of this
code exists, all db interaction is handled by the SqlDataSource object
behind the scenes.
the whole point in trying this approach was to get away from all the hand
coding.
and it does work very well as long as you don't run into any db exceptions.

Gerry
 
G

gerry

tried it where ?
all of the db update code is hidden away in the SqlDataSource object and
there is no place to use a try/catch.
 
G

gerry

i did get this working , but its pretty kludgy

in OnError, I save my error message in the session, i do a Server.Transfer
back to the same page with form preservation. in Page_Load I look for the
message in the session, if its there I display it, remove it from the
session and cancel any further processing / updates.

I still have to wonder why there is no OnError associated with the
SqlDataSource object.

Gerry
 
G

Guest

Hello Gerry,

You should be able to add a handler for the relevant event: selected,
updated, inserted, deleted. Then, in the handler look at the
SqlDataSourceStatusEventArgs property Exception. If it's not null then an
exception has occurred. For example if the selected command threw an
exception:

protected void SqlDataSource1_Selected(object sender,
SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
// handle the exception
}
}
 
G

gerry

Thanks Brian , that did it.


brians said:
Hello Gerry,

You should be able to add a handler for the relevant event: selected,
updated, inserted, deleted. Then, in the handler look at the
SqlDataSourceStatusEventArgs property Exception. If it's not null then an
exception has occurred. For example if the selected command threw an
exception:

protected void SqlDataSource1_Selected(object sender,
SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
// handle the exception
}
}
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top