Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2009, 01:41 PM
Authorized User
 
Join Date: Mar 2009
Posts: 64
Thanks: 0
Thanked 1 Time in 1 Post
Default Throwing an exception from a SqlDataSource

Imar,

My Default.aspx has a GridView connected to a SqlDataSource. All is well if I can connect to the data source, which is done as the page is loading. If I cannot, for whatever reason, how do I throw an exception and handle it gracefully?

I have looked at the members of the SqlDataSource class and did not see anything I could use in Page_Load, for example.

Thomas
 
Old April 28th, 2009, 04:26 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Thomas,

Not sure what you're asking exactly. The controls do already throw exceptions themselves when the database cannot be reached.

Some of the events (like Inserted) have an e argument with an Exception argument that you can check (and optionally handle by setting e.ExceptionHandled to True). The control fills the that property if it encounters a non-fatal exception.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 29th, 2009, 10:38 AM
Authorized User
 
Join Date: Mar 2009
Posts: 64
Thanks: 0
Thanked 1 Time in 1 Post
Default

Imar,

My confusion comes from this. I have a SqlSource that runs as the page opens to display data from which the user can chose. It is set up to bind and get some data from a specific database. All of that is "buried" in the SqlSource control, i.e., in the configuration of the data source.

If I run the application when the database is not available there is no code in the "Code Behind" to handle the exception. What event code should I create to handle the exception? For example, the "Databinding" event says that it "Fires when the controls databinding expressions are about to be evaluated." So if the control cannot bind I do not get the exception, right?

Perhaps I am having a conceptual problem, but I cannot see how to know that the database was not reached and inform my user gracefully.

Thomas
 
Old April 29th, 2009, 03:25 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Ah, I see. You have a few ways to handle this.

1. Use Page_Error to locally handle errors. A quick example:

Code:
 
protected void Page_Error(object sender, EventArgs e)
{
  Response.Write("Ooops");
  Context.ClearError();
}
This is a simple example that writes out a message. It's important to call ClearError or you still see the error on the page.

2. Globally handle erorrs. For more info, take a look at Chapter 17.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Throwing Security Exception Error Manoj Bisht ASP.NET 3.5 Professionals 2 March 28th, 2009 01:52 AM
SelectSingleNode throwing exception: anup_daware .NET Framework 2.0 1 February 3rd, 2008 12:04 PM
Axis client throwing exception sometimes chandootechie J2EE 0 March 17th, 2006 07:51 AM
RangeValidaor throwing exception anup_daware .NET Framework 1.x 0 March 16th, 2006 07:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.