Solved

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

Posted on 2006-06-15
13
1,522 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
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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now