[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

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?
Thankyou
Anat
0
ANAT2403
Asked:
ANAT2403
  • 5
  • 5
1 Solution
 
raterusCommented:
Anat, can you post your aspx code?  I'm a bit confused as to what exactly you are doing with this dropdownlist
0
 
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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">
            <DeleteParameters>
                <asp:Parameter Name="ProductID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="CategoryID" Type="Int32" />
                <asp:Parameter Name="ProductID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="CategoryID" Type="Int32" />
            </InsertParameters>
            <SelectParameters>
                <asp:Parameter DefaultValue="7" Name="ProductID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
   
    </div>
        <asp:SqlDataSource ID="SDSCategories" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] WHERE ([CategoryID] > @CategoryID)">
            <SelectParameters>
                <asp:Parameter DefaultValue="8" Name="CategoryID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
            DataSourceID="SDSProduct" Style="z-index: 100; left: 328px; position: absolute;
            top: 136px" AllowPaging="True">
            <Columns>
                <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">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DDLCategoriesEdit" runat="server" AutoPostBack="True" DataSourceID="SDSCategories"
                            DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'
                            Width="136px">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="DDLCategoriesSel" runat="server" AutoPostBack="True" DataSourceID="SDSCategories"
                            DataTextField="CategoryName" DataValueField="CategoryID" Enabled="False" Width="136px" SelectedValue='<%# Bind("CategoryID") %>'>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>


Thanks
Anat
0
 
raterusCommented:
hmm..so if there is an error binding, you want to display the user a friendly message, and not the standard ASP.net 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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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?
 
0
 
raterusCommented:
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.
0
 
ANAT2403Author Commented:
O.K. suppose I am not using the datasource controls. What then I should do?
Anat
0
 
raterusCommented:
I'm not saying you shouldn't use the datasource controls, but I am asp.net 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.
0
 
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?
0
 
raterusCommented:
I would do something like this,
http://support.microsoft.com/default.aspx?scid=kb;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.
0
 
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?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now