473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

catch sql exception

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


Jul 6 '06 #1
7 2373
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

Jul 6 '06 #2
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
<ge*********@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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

Jul 6 '06 #3
"gerry" <ge**@nospam.nospamwrote in message
news:OE**************@TK2MSFTNGP05.phx.gbl...
and it does work very well as long as you don't run into any db
exceptions.
Have you tried:

Catch ex as SqlException

Jul 6 '06 #4
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.
"Mark Rae" <ma**@markN-O-S-P-A-M.co.ukwrote in message
news:OS**************@TK2MSFTNGP04.phx.gbl...
"gerry" <ge**@nospam.nospamwrote in message
news:OE**************@TK2MSFTNGP05.phx.gbl...
and it does work very well as long as you don't run into any db
exceptions.

Have you tried:

Catch ex as SqlException

Jul 6 '06 #5
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

"gerry" <ge**@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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


Jul 6 '06 #6
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
}
}
--
enjoy - brians
http://www.limbertech.com
"gerry" wrote:
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


Jul 6 '06 #7
Thanks Brian , that did it.
"brians[MCSD]" <br********@discussions.microsoft.comwrote in message
news:ED**********************************@microsof t.com...
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
}
}
--
enjoy - brians
http://www.limbertech.com
"gerry" wrote:
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


Jul 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
by: Jacek Dziedzic | last post by:
Hi! In my main() function I have a last-resort exception construct that looks like this: int main() { try { // ... program code }
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
2
by: Ralph Krausse | last post by:
I created a try/catch/finally but when an expection is thrown, the catch does not handle it... (I know this code is wrong, I want to force the error for this example) try { DataSet ds = new...
11
by: l.woods | last post by:
I want to set up my CATCH for a specific exception, but I really don't know which one of the multitude that it is. I am getting the exception now with Catch ex as Exception but I want to be...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.