Solved

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Posted on 2006-06-15
13
1,539 Views
Last Modified: 2012-08-13
I have the following code that works fine 95% of the time, however it keeps throwing

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Points to anyone who helps me resolve the problem as I dont know where to look, it works most of the time?
Please post for any other info that may help.

            If MyDataSet.Tables(0).Rows.Count <> 0 Then
>>>            If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("answer_id")) Then rdoAnswers.SelectedValue = MyDataSet.Tables(0).Rows(0).Item("answer_id") Else rdoAnswers.SelectedValue = ""
            If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("comments")) Then txtComments.Text = MyDataSet.Tables(0).Rows(0).Item("comments")
            Else
                txtComments.Text = ""
            End If
0
Comment
Question by:tomjbarnard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 2

Expert Comment

by:cmjwebservices
ID: 16909865
Write it as follows:

Try
If MyDataSet.Tables(0).Rows.Count <> 0 Then
If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("answer_id")) Then rdoAnswers.SelectedValue = MyDataSet.Tables(0).Rows(0).Item("answer_id") Else rdoAnswers.SelectedValue = ""
            If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("comments")) Then txtComments.Text = MyDataSet.Tables(0).Rows(0).Item("comments")
            Else
                txtComments.Text = ""
            End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
Return
End Try

This will give you the exact details of the error when thrown.  If you wish to give a custom message, replace "ex.ToString" with something like "An error has occurred.  Please try the operation again." and make sure to place "Return" or "Exit Sub" after the exception message if you don't want anything else to execute after the error has occurred.
0
 

Author Comment

by:tomjbarnard
ID: 16910075
Thanks, this makes sense.

I have added in the code to catch the exception and will post the results when its reported.
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16910176
H,
plz use MyDataSet.Tables(0).Rows.Count-1 <> 0.

then your problem will be solved.

we can access 0 to MyDataSet.Tables(0).Rows.Count-1 only.
MyDataSet.Tables(0).Rows.Count is a total number of rows
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Expert Comment

by:manch
ID: 16910581
Probably the Dataset does not contain any table in some cases

So u must first include the Try .... Catch Block to ur code

and secondly
write the following if statement :

if MyDataSet.Tables.count <> 0 then

           If MyDataSet.Tables(0).Rows.Count <> 0 Then
>>>            If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("answer_id")) Then rdoAnswers.SelectedValue = MyDataSet.Tables(0).Rows(0).Item("answer_id") Else rdoAnswers.SelectedValue = ""
            If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("comments")) Then txtComments.Text = MyDataSet.Tables(0).Rows(0).Item("comments")
            Else
                txtComments.Text = ""
            End If

end if
0
 

Author Comment

by:tomjbarnard
ID: 16918538
OK,

cmjwebservices's try catch was added however we get the same error message (and no msgbox)



Specified argument was out of the range of valid values. Parameter name: 420
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: 420

Source Error:
 
Line 311:
Line 312:        Try
Line 313:        If MyDataSet.Tables(0).Rows.Count <> 0 Then
Line 314:            If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("answer_id")) Then rdoAnswers.SelectedValue = MyDataSet.Tables(0).Rows(0).Item("answer_id") Else rdoAnswers.SelectedValue = ""
Line 315:            If Not IsDBNull(MyDataSet.Tables(0).Rows(0).Item("comments")) Then txtComments.Text = MyDataSet.Tables(0).Rows(0).Item("comments")

Source File: c:\inetpub\wwwroot\gemini\popups\questionnaire.aspx.vb    Line: 313

Stack Trace:
 
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: 420]
   System.Web.UI.WebControls.ListControl.set_SelectedValue(String value) +152
   gemini.questionnaire.ShowResults(Int32 QuestionNum) in c:\inetpub\wwwroot\gemini\popups\questionnaire.aspx.vb:313
   gemini.questionnaire.ShowQuestion(String QuestionID) in c:\inetpub\wwwroot\gemini\popups\questionnaire.aspx.vb:306
   gemini.questionnaire.lnkGreaterThan_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\gemini\popups\questionnaire.aspx.vb:807
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
   System.Web.UI.Page.ProcessRequestMain() +1292
 
________________________________________
Version Information: Microsoft .NET Framework Version:1.1.4322.2300; ASP.NET Version:1.1.4322.2300
0
 

Author Comment

by:tomjbarnard
ID: 16919021
manch,

I have tried

MyDataSet.Tables.count <> 0 then

in place of

MyDataSet.Tables(0).Rows.Count <> 0 then

same error :(
0
 
LVL 2

Expert Comment

by:cmjwebservices
ID: 16919081
"MyDataSet.Tables(0).Rows.Count - 1 <> 0"

Did you try pradeepsudharsan's suggestion above?
0
 
LVL 2

Expert Comment

by:cmjwebservices
ID: 16919095
I also found this, which I thought would be applicable since you are using a ListControl with selectedvalue property:

"I figured out the problem; based on a complex series of events with the dynamic controls, the DropDownList.SelectedValue property was getting set before the data was loaded. This caused the DataBind to fail. Why the DropDownList decides to do this, I don't know, it sure would have been easier to find if it just told me it couldn't select that item when I tried to set it :)"

Source: http://www.dotnet247.com/247reference/msgs/40/204642.aspx
0
 

Author Comment

by:tomjbarnard
ID: 16919281
I have change the code to use the -1, waiting for userfeed back
0
 
LVL 2

Accepted Solution

by:
cmjwebservices earned 500 total points
ID: 16919323
In addition, it seems that your Else clause tries to set SelectedValue = "", which doesn't exist unless the ListItem contains a null value.   If the above doesn't work, do this (which I found and translated to vb.net from "http://aspzone.com/blogs/john/archive/2006/05/09/1775.aspx"):

<asp:RadioButtonList ID="RadioButtonList" runat="server"
   SelectedValue='<%# Bind("Whatever") %>'
   OnDataBound="RadioButtonList_DataBound">
      <asp:ListItem Value=""></asp:ListItem>
      <asp:ListItem Value="A"></asp:ListItem>
      <asp:ListItem Value="B"></asp:ListItem>
      <asp:ListItem Value="C"></asp:ListItem>
</asp:RadioButtonList>

Protected Sub MyRadioButtonList_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        Dim list As RadioButtonList = CType(sender, RadioButtonList)
        Dim blank As ListItem = list.Items.FindByValue("")
        If Not blank Is Nothing Then
             list.Items.Remove(blank)
        End If
End Sub

Quote: "This way the databinding code has a ListItem to set as selected. Then after that code is done, my DataBound event handler runs, and removes the blank item from the list."
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question