VB.NET web app - dropdownlist index problem - can't choose index 0!


I have cmbSearchby, a dropdownlist that selects between "nomenclature" and "item id" -
cmbSearch1 is for nomenclature, and is bound to the productname field of table products
cmbSearch2 is for item ID, and is bound to the productcode field of table products
cmbSearchby decides which of those two boxes shows, but either one can be used to choose from the same records.

I have several text boxes and a couple combos.  These correspond to the fields in table Products.  These I want to fill in manually, since I can't properly figure out how to bind them (the text boxes).  The two combos are filled with values from other tables, Units and Suppliers.  My code is as follows:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Session("files") = 0 Then cmdDelete.Visible = False

        cmdDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this element ?');")

        'Put user code to initialize the page here


        cmbSup.DataBind()     ' Bind supplier combo
        cmbUIss.DataBind()     ' Bind unit issue combo

        If Not (IsPostBack) Then
            cmbSearchBy.Items.Add("Item ID")

            cmbSearch1.Visible = True
            cmbSearch2.Visible = False
            lblSearch1.Visible = True
            lblSearch2.Visible = False

            cmbUIss.SelectedValue = "EA"


        Else ' Postback

        End If

    End Sub

    Private Sub cmbSearchBy_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSearchBy.SelectedIndexChanged
        If lblSearch1.Visible = True Then
            lblSearch1.Visible = False
            lblSearch2.Visible = True
            cmbSearch1.Visible = False
            cmbSearch2.Visible = True
            lblSearch1.Visible = True
            lblSearch2.Visible = False
            cmbSearch1.Visible = True
            cmbSearch2.Visible = False
        End If
    End Sub

    Private Sub FillControls()
        Dim selIndex = -1

        For a As Integer = 0 To DS_Products1.Tables(0).Rows.Count - 1
            If DS_Products1.Tables(0).Rows(a).Item("ProductName") = cmbSearch1.SelectedValue Then
                selIndex = a
            End If

        If selIndex = -1 Then Exit Sub

        txtID.Text = DS_Products1.Tables(0).Rows(selIndex).Item("ProductCode")
        cmbSup.SelectedValue = DS_Products1.Tables(0).Rows(selIndex).Item("SupplierCode")
        txtNom.Text = DS_Products1.Tables(0).Rows(selIndex).Item("ProductName")
        txtManf.Text = DS_Products1.Tables(0).Rows(selIndex).Item("Manufacturer")
        txtModel.Text = DS_Products1.Tables(0).Rows(selIndex).Item("Model")
        txtUPri.Text = DS_Products1.Tables(0).Rows(selIndex).Item("UnitPrice")
        cmbUIss.SelectedValue = DS_Products1.Tables(0).Rows(selIndex).Item("qty_issue")
    End Sub

I still get the same error - everything works until the FillControls sub - this one chooses the index by running a search in the dataset table to find the value that's pointed at by cmbSearch1.  When I run the program I get:

Server Error in '/OEL2' Application.

Specified argument was out of the range of valid values. Parameter name: 1
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: 1

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: 1]
   System.Web.UI.WebControls.ListControl.set_SelectedValue(String value) +152
   OEL2.DBItem.FillControls() in \\ga1cdc02\c$\Inetpub\wwwroot\OEL2\DBItem.aspx.vb:454
   OEL2.DBItem.Page_Load(Object sender, EventArgs e) in \\ga1cdc02\c$\Inetpub\wwwroot\OEL2\DBItem.aspx.vb:415
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +750


I have no idea how to fix this - even if I build a buffer into cmbSearch1 and cmbSearch2, then when someone chooses that buffer (say, "---" or something), the program will crash because index 0 will have been chosen.  What could I be doing to elicit this problem?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


To select an item into a DropDownList, use the following function.
You can it to avoid this kind of things : when you want to set the value of a combobox that doesn't contains the value, it automatically insert it before selecting it.

    Public Sub SelectItem(ByVal Control As DropDownList, ByVal Code As String)
        Dim Item As ListItem

        Item = Control.Items.FindByValue(Code)
        ' Or use the following if this is the text that you want to choose
        ' Item = Control.Items.FindByText(Code)

        If Not Item Is Nothing Then
            Dim SelItem As ListItem = Control.SelectedItem

            SelItem.Selected = False
            Item.Selected = True
            Control.SelectedValue = Code
        End If
    End Sub

Customize it at your own will


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gleznovAuthor Commented:
Can you give me a little more info on how to use that in my program?  

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.