catch OutOfRangeException in sqldatasource

In ASP.NET 2.0 with C# I have a gridview. one of the columns is a template of dropdownlist.
I have 2 sqldatasource in the page. one for the gridview and one for the dropdownlist.
The dropdownlist is suppose to show a value that does nor exist in its sqldatasource.
Where and how I catch this execption of System.ArgumentOutOfRangeException or how do I add a validation control
to the gridview or how do I show an error message of my own and not just get the screen error of VS2005?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anat, can you post your aspx code?  I'm a bit confused as to what exactly you are doing with this dropdownlist
ANAT2403Author Commented:
I created a sample in Northwind.
The main idea is that there is a dropdownlist in a gridview that is bound to a field that does not exist and I want to show a message of this error without being thown away from my application.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testDropDownList.aspx.cs" Inherits="testDropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:SqlDataSource ID="SDSProduct" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [CategoryID] FROM [Products] WHERE ([ProductID] < @ProductID)" DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [CategoryID]) VALUES (@ProductName, @CategoryID)" UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID WHERE [ProductID] = @ProductID">
                <asp:Parameter Name="ProductID" Type="Int32" />
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="CategoryID" Type="Int32" />
                <asp:Parameter Name="ProductID" Type="Int32" />
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="CategoryID" Type="Int32" />
                <asp:Parameter DefaultValue="7" Name="ProductID" Type="Int32" />
        <asp:SqlDataSource ID="SDSCategories" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] WHERE ([CategoryID] > @CategoryID)">
                <asp:Parameter DefaultValue="8" Name="CategoryID" Type="Int32" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
            DataSourceID="SDSProduct" Style="z-index: 100; left: 328px; position: absolute;
            top: 136px" AllowPaging="True">
                <asp:CommandField ShowSelectButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <asp:TemplateField HeaderText="Category Name" SortExpression="CategoryID">
                        <asp:DropDownList ID="DDLCategoriesEdit" runat="server" AutoPostBack="True" DataSourceID="SDSCategories"
                            DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'
                        <asp:DropDownList ID="DDLCategoriesSel" runat="server" AutoPostBack="True" DataSourceID="SDSCategories"
                            DataTextField="CategoryName" DataValueField="CategoryID" Enabled="False" Width="136px" SelectedValue='<%# Bind("CategoryID") %>'>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />

raterusCommented: if there is an error binding, you want to display the user a friendly message, and not the standard error page.  Do you still want them to be able to do other things on this page after seeing this error?  I'm going to tell you, it's going to be a lot easier displaying a custom error page than it is going to be to catch that binding error.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ANAT2403Author Commented:
I want them be able to do other things on this page after seeing this error.
How am I suppose to do this?
Can I use the gridview event GridView1_RowDataBound ? or is there another place?
I know that if there is an error in the update event I can catch it in GridView1_RowUpdated and
then use the command e.KeepInEditMode = true to keep working. What do I in my situation
that the gridview is just loading and there is an error?
If you weren't using the datasource controls, I could tell you what needs to be done, but as it stands, I don't even know where to begin to tell you to begin to do this.  Why would you be binding a column that isn't there?  You shouldn't design your programs around errors/exceptions as this is very inefficient.
ANAT2403Author Commented:
O.K. suppose I am not using the datasource controls. What then I should do?
I'm not saying you shouldn't use the datasource controls, but I am programmer that chooses not to use them.  I'd be more concerned about why this is breaking in the first place, and what can you do about it BEFORE it breaks, not assuming it will break and working around it.
ANAT2403Author Commented:
My database contain data that is imported from an old database and there might be some incompatability. I try to cover
all the situation but if something still not match I don't want the user to have the microsoft error page that goes out of the application. I want to have an error message an continue. So what I am asking now is how to have
this error message and continue with the program? Can you tell me this?
I would do something like this,;en-us;306355

You can point your error page to a page that will have navigation, and the user doesn't have to know they ever navigated away from your application.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ANAT2403Author Commented:
O.K. It helps me. The fact that all this is for ASP.NET 1.0 doesn't matter?
In ASP.NET 2.0 we are suppose to use the same system?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.