Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 22nd, 2006, 02:30 PM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default SqlDataSource - Error Handling?

I've got a bunch of ASP.NET books which cover this control, but none of them discuss error handling. For example, what is the preferred method for handling a situation where you're trying to do a query through a SqlDataSource object, but the database server is temporarily down?

In doing a Google search, I see that this a common question, but there doesn't seem to be any consensus as to how to handle this. It also seems like an oversight on Microsoft's part that there are no built-in error events for the control.

What I'm really trying to figure out is: if you're creating a public web site and you want it to be bullet-proof, should you not use this control and just stick to code for database connectivity?
Reply With Quote
  #2 (permalink)  
Old September 23rd, 2006, 06:46 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,044
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Hi Rob,

You can hook into various events of the SqlDataSource control, like its Selected, Updated, Deleted events and so on.

These events have an EventArgs argument that have an Exception property. This property is not null / nothing when an error occurred somehow. By diagnosing the exception, you can take appropriate action, or decide to let the exception bubble up. The following code will be triggered if, for example, the server cannot be found:
Code:
  Protected Sub SqlDataSource1_Selected(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) _
            Handles SqlDataSource1.Selected

    If e.Exception IsNot Nothing Then
      ' Do Something with the Exception, like logging

      ' Optionally, set ExceptionHandled to True or False so the 
      ' Exception is swallowed or forwarded.
      e.ExceptionHandled = True
    End If

  End Sub
  I agree it would be much easier if there was some sort of ExceptionOccurred event that you could handle in a single location, Then again, you'd be stuck with a general exception, not necessarily directly related to the action you were carrying out like selecting or deleting.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #3 (permalink)  
Old September 23rd, 2006, 05:04 PM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for your insight.

I'm curious to know, though: how do you handle database connectivity in your projects? I've been learning about the DataReader and DataAdapter objects along with the different ways to bind data, and I just came across an example of creating and using a SqlDataSource object entirely through code, which never occurred to me (C#):
Code:
protected void Page_Load(object sender, EventArgs e)
    {
      SqlDataSource myDataSource = null;

      if (!Page.IsPostBack)
      {
        myDataSource = new SqlDataSource();
        myDataSource.ConnectionString = ConfigurationManager.
            ConnectionStrings["MyConnectionString"].ConnectionString;
        myDataSource.DataSourceMode = SqlDataSourceMode.DataReader;
        myDataSource.ProviderName = "System.Data.SqlClient";
        myDataSource.SelectCommand = "SELECT FirstName, LastName FROM Users";
        myDataGrid.DataSource = myDataSource;
        myDataGrid.DataBind();
        }
    }
This seems like an elegant solution (at least that is my impression, but I could be wrong...), because you get the convenience of the SqlDataSource's built-in methods plus the ability to wrap the database call in a Try/Catch statement.

It's nice to know that there are all of these possible solutions. However, at this point in the learning process I would like to pick the best approach to a given problem and build on it - this would help me in building my reusable code library, which is something I create whenever I'm learning a new language. The problem is, I'm unsure of which way(s) to go when it comes to accessing databases. This is why I'd be interested in hearing how experienced developers do things.

Thanks,

Rob
Reply With Quote
  #4 (permalink)  
Old September 24th, 2006, 04:48 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,044
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Hi Rob,

Personally, I don't use the SqlDataSource that often.
It breaks three-tier design because the presentation layer (the ASPX pages) talk to the database directly. In some scenarios (demos, prototypes, small applications) this is acceptable, but for larger applications it can be problematic.

We often use an ObjectDataSource instead. Presentation objects (GridViews, Repeaters and other controls) are tied to this ODS that in turn talks to a custom Business Layer, inside a separate Class Library project (or directly in the App_Code folder for smaller projects). For all data access, this Business Layer talks to a separate Data Access Layer, In complex scenarios, this DAL can be custom code specifically written for the project. In other situations, we use a database helper class that allows us to easily execute SQL statements against the database, to change and retrieve data in the database. The concepts we're using are somewhat similar to the DataBase helper class found here:

http://www.binaryintellect.net/artic...icle.aspx?id=1

With the code you posted, I think you loose some of the benefits of the SqlDataSource. It's no longer able to figure out if and when it needs to load its data since you manually do the databinding. Once you start writing code like this, you might as well write your own business objects that access the database.

It sounds like a plug (and I guess it is) but my latest book ASP.NET 2.0 Instant Results has many examples of the way we build real-world applications.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #5 (permalink)  
Old September 25th, 2006, 11:33 AM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah, More things to think about...

Thanks, Imar. I did check out your book, but I saw the code was only VB, and I have my hands full as it is learning C#. I'll take another look.:)
Reply With Quote
  #6 (permalink)  
Old September 25th, 2006, 12:51 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,044
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Syntax is only a very small part of the book. It deals with concepts and general technologies much more than with pure syntax. So, if you have an on-line code converter, or you're able to imagine semi-colons ending each line, I think you should come a long way with understanding the book...

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SqlDataSource control error binici ASP.NET 2.0 Basics 1 January 19th, 2007 08:02 PM
SqlDatasource DeleteCommand gving error Prashant.k.m .NET Framework 2.0 2 May 5th, 2005 12:29 AM
Error handling Hudson40 Access VBA 2 February 11th, 2005 05:32 AM
Error Handling zaeem Classic ASP Basics 2 November 14th, 2003 10:51 AM
Error handling ! ittorget Classic ASP Professional 2 November 14th, 2003 09:44 AM



All times are GMT -4. The time now is 01:27 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.