Click here to Skip to main content
15,885,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good Afternoon

I'm hoping someone can assist as I'm obviously missing something here.

Basically I have a drop down list of a category when viewing in the webpage and selecting a job with a category its coming up with an error

An exception of type 'System.ArgumentOutOfRangeException' occurred in System.Web.dll but was not handled in user code

My code is below and it refers to the line DDCat

VB
Protected Sub DDOFIREF_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDOFIREF.SelectedIndexChanged

      'LblRef.Text = DDOFIREF.Text

      Dim dv As New Data.DataView 'use the id of your SqlDataSource below'
      dv = AccessDataSource3.Select(DataSourceSelectArguments.Empty)
      LblUser.Text = dv.Table.Rows(0)("UserN")
      LblDept.Text = dv.Table.Rows(0)("Dept")
      lblroot.Text = dv.Table.Rows(0)("Root")
      lblassign.Text = dv.Table.Rows(0)("AssignTo")
      txtdetails.Text = dv.Table.Rows(0)("Details")
      txtctc.Text = dv.Table.Rows(0)("CostCompany")
      lblref2.Text = dv.Table.Rows(0)("ref")
      DDCat.Text = If(IsDBNull(dv.Table.Rows(0)("Category")), DDCat.SelectedItem.ToString = "Select one", dv.Table.Rows(0)("Category"))
      txtact.Text = If(IsDBNull(dv.Table.Rows(0)("Action")), String.Empty, dv.Table.Rows(0)("action"))
      txtofi.Text = If(IsDBNull(dv.Table.Rows(0)("OfiNotes")), String.Empty, dv.Table.Rows(0)("OfiNotes"))


Any help would be greatly appreciated.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jun-15 11:58am    
Check if the number of rows in dv.Table is zero...
—SA
Emm4 11-Jun-15 12:23pm    
Hi Sergey, I have checked and they are set at 0 .....
Sergey Alexandrovich Kryukov 11-Jun-15 12:31pm    
What do you mean by that? If count is zero, indexing element at (0) will throw this exception.
After all, run it under the debugger.
—SA

1 solution

Whenever you are getting data from a database you should always check that the query has returned something before attempting to use it. For example
VB
Protected Sub DDOFIREF_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DDOFIREF.SelectedIndexChanged

    'LblRef.Text = DDOFIREF.Text

    Dim dv As New Data.DataView 'use the id of your SqlDataSource below'
    dv = AccessDataSource3.Select(DataSourceSelectArguments.Empty)

    If dv.Table.Rows.Count > 0 Then
        LblUser.Text = dv.Table.Rows(0)("UserN")
        LblDept.Text = dv.Table.Rows(0)("Dept")
        lblroot.Text = dv.Table.Rows(0)("Root")
        lblassign.Text = dv.Table.Rows(0)("AssignTo")
        txtdetails.Text = dv.Table.Rows(0)("Details")
        txtctc.Text = dv.Table.Rows(0)("CostCompany")
        lblref2.Text = dv.Table.Rows(0)("ref")
        DDCat.Text = If(IsDBNull(dv.Table.Rows(0)("Category")), DDCat.SelectedItem.ToString = "Select one", dv.Table.Rows(0)("Category"))
        txtact.Text = If(IsDBNull(dv.Table.Rows(0)("Action")), String.Empty, dv.Table.Rows(0)("action"))
        txtofi.Text = If(IsDBNull(dv.Table.Rows(0)("OfiNotes")), String.Empty, dv.Table.Rows(0)("OfiNotes"))
    End If

End Sub

Simply put, if there are no rows returned then Rows(0) does not exist (nor Rows(1), Rows(2) etc) - and there is your exception being thrown.

It's worth checking how you have configured the AccessDataSource control - try running the query directly against the database to see what you get back.
 
Share this answer
 
v2
Comments
Emm4 12-Jun-15 4:42am    
Hi CHill60, thanks for sending that over. Basically I am wanting something to be returned .... I have tested it by entering data into the webpage in every field and it works fine but as soon as I leave a field empty and select that ref from the dropdown it throws this exception up.

I am now however getting this error message

'DDCat' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
CHill60 12-Jun-15 8:06am    
The category that has been returned from the database is not in the list of items for that dropdown. You can't assign a value to .Text unless the Items already contains that value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900