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

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
tomjbarnardAsked:
Who is Participating?
 
cmjwebservicesConnect With a Mentor Commented:
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
 
cmjwebservicesCommented:
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
 
tomjbarnardAuthor Commented:
Thanks, this makes sense.

I have added in the code to catch the exception and will post the results when its reported.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
pradeepsudharsanCommented:
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
 
manchCommented:
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
 
tomjbarnardAuthor Commented:
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
 
tomjbarnardAuthor Commented:
manch,

I have tried

MyDataSet.Tables.count <> 0 then

in place of

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

same error :(
0
 
cmjwebservicesCommented:
"MyDataSet.Tables(0).Rows.Count - 1 <> 0"

Did you try pradeepsudharsan's suggestion above?
0
 
cmjwebservicesCommented:
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
 
tomjbarnardAuthor Commented:
I have change the code to use the -1, waiting for userfeed back
0
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.

All Courses

From novice to tech pro — start learning today.