Solved

Web Apps, listbox indexes - start at 0 or 1?

Posted on 2004-10-27
211 Views
Last Modified: 2010-04-23
Hi,

     I've been programming Windows apps in .NET for awhile now, but web apps are new.  I'm working on one right now that's giving me an error:

Specified argument was out of the range of valid values. Parameter name: 1

All I know is when I select any index initially other than 0 to fill in text boxes with, I have no problem.  When I choose index 0, I get this error.  Are you not supposed to use index 0 in a listbox or other control in a web app?  And if that is the case, then my other problem is that index 1 chooses the second item, so that whatever's first in the listbox must (in my mind) be index 0, but I can't choose it in code.  I'm so frustrated.

JP
0
Question by:gleznov
    16 Comments
     
    LVL 5

    Expert Comment

    by:jpontani
    Try using the LBound and UBound functions.  These get the lower and upper bounds of the array, which a listbox inherits.  If you want the first item use LBound(listbox) to return the first usable index.  You can use these as loop controls, they come in handy if you want to loop through the items without having to know what index to start at.

    - Joe
    0
     

    Author Comment

    by:gleznov
    LBound(listbox) doesn't seem to work - is it LBound(listbox.something)?

    JP
    0
     
    LVL 5

    Expert Comment

    by:jpontani
    Sorry, it should be LBound(listbox.items).  Also, replace 'listbox' with the name of your listbox.
    0
     

    Author Comment

    by:gleznov
    Still a problem - I'm using:
    selIndex = LBound(cmbSearch.Items)

    it still doesn't like it:

    \\ga1cdc02\c$\Inetpub\wwwroot\OEL2\DBItem.aspx.vb(432): Value of type 'System.Web.UI.WebControls.ListItemCollection' cannot be converted to 'System.Array'.

    cmbSearch is a dropdownlist, I just still use cmb (combo) as my prefix because that's what I'm used to using.  Oh, I see above I've been saying listbox - sorry bout that, it's actually a dropdownlist.

    JP
    0
     
    LVL 5

    Expert Comment

    by:jpontani
    This thought just popped in my head.  If you want the selected item to be the first item in the list, why do you need to set the selectedIndex?  When the page loads if there is no preset selectedIndex, the selected item is automatically set to be the first item in the list.
    0
     

    Author Comment

    by:gleznov
    Well that's true, but I need to manually fill some text and combo controls using the record in the database referenced by the selected element in that combo box.  I was just setting it to 0 for the initial load, but that's where it didn't work.  I've tried binding, but for some reason I'm getting nothing in bound boxes.  

    JP
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    It should start with 0, i tried and it works fine.

    Can you post your code, including the place (procedure) where you are setting the selectedIndex?
    0
     

    Author Comment

    by:gleznov
    Here is the page_load sub - I used >> in front of the part that crashes with that error.



            Session("IsAdd") = False
                Dim selIndex As Integer = -1

                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

                SqlDataAdapter1.Fill(DS_DBItem1)
                SqlDataAdapter2.Fill(DS_DBItem1)
                SqlDataAdapter3.Fill(DS_DBItem1)


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

                    ' Fill in the search combos
                cmbSearch.Items.Clear()
                If cmbSearchBy.SelectedValue = "Nomenclature" Then
                    lblSearch.Text = "Nomenclature"
                    For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                        cmbSearch.Items.Add(DS_DBItem1.Tables("products").Rows(x).Item("ProductName"))
                    Next
                Else
                    lblSearch.Text = "Item ID"
                    For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                        cmbSearch.Items.Add(DS_DBItem1.Tables("products").Rows(x).Item("ProductCode"))
                    Next
                End If

                ' Fill in the supplier combo
                For x As Integer = 0 To DS_DBItem1.Tables("suppliers").Rows.Count - 1
                    cmbSup.Items.Add(DS_DBItem1.Tables("suppliers").Rows(x).Item("CompanyName"))
                Next

                ' Fill in the Unit Issue combo
                For x As Integer = 0 To DS_DBItem1.Tables("units").Rows.Count - 1
                    cmbUIss.Items.Add(DS_DBItem1.Tables("units").Rows(x).Item("unit"))
                Next

                cmbUIss.SelectedValue = "EA"

                ' Disable controls
                txtID.Enabled = False
                cmbSup.Enabled = False
                txtNom.Enabled = False
                txtManf.Enabled = False
                txtModel.Enabled = False
                txtUPri.Enabled = False
                cmbUIss.Enabled = False

                ' Setup Buttons
                cmdAdd.Enabled = True
                cmdEdit.Enabled = True
                cmdSave.Enabled = False
                cmdSave.ImageUrl = "oel2_btn_save_disabled.bmp"
                cmdCancel.Enabled = False
                cmdCancel.ImageUrl = "oel2_btn_cancel_disabled.bmp"
                cmdDelete.Enabled = True
                cmdMainMenu.Enabled = True

                ' Fill controls
                ' =============
                ' Fill controls
    >>            ' selIndex = 0
    >>            selIndex = LBound(cmbSearch.Items)
    >>            txtID.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductCode")
    >>            txtNom.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductName")
    >>            txtManf.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Manufacturer")
    >>            txtModel.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Model")
    >>            txtUPri.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("UnitPrice")
    >>            cmbUIss.SelectedValue = DS_DBItem1.Tables("products").Rows(selIndex).Item("Qty_Issue")

                ' Determine supplier from ID code
                Dim supIndex As Integer = -1
                For x As Integer = 0 To DS_DBItem1.Tables("suppliers").Rows.Count - 1
                    If DS_DBItem1.Tables("suppliers").Rows(x).Item("SupplierID") = DS_DBItem1.Tables("products").Rows(selIndex).Item("SupplierCode") Then
                        supIndex = x
                        Exit For
                    End If
                Next
                cmbSup.SelectedValue = DS_DBItem1.Tables("Suppliers").Rows(supIndex).Item("CompanyName")
            Else ' Is Postback
                ' Find selIndex
                If cmbSearchBy.SelectedValue = "Nomenclature" Then
                    For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                        If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductName") Then
                            selIndex = x
                            Exit For
                        End If
                    Next
                Else
                    For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                        If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductCode") Then
                            selIndex = x
                            Exit For
                        End If
                    Next
                End If
                ' Fill controls
                txtID.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductCode")
                txtNom.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductName")
                txtManf.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Manufacturer")
                txtModel.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Model")
                txtUPri.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("UnitPrice")
                cmbUIss.SelectedValue = DS_DBItem1.Tables("products").Rows(selIndex).Item("Qty_Issue")
            End If

        End Sub

        Private Sub cmbSearchBy_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSearchBy.SelectedIndexChanged
            cmbSearch.Items.Clear()
                If cmbSearchBy.SelectedValue = "Nomenclature" Then
                    lblSearch.Text = "Nomenclature"
                    For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                        cmbSearch.Items.Add(DS_DBItem1.Tables("products").Rows(x).Item("ProductName"))
                    Next
                Else
                    lblSearch.Text = "Item ID"
                    For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                        cmbSearch.Items.Add(DS_DBItem1.Tables("products").Rows(x).Item("ProductCode"))
                    Next
                End If
        End Sub

        Private Sub cmdMainMenu_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdMainMenu.Click
            Response.Redirect("MainMenu.aspx")
        End Sub

        Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdAdd.Click
            Session("IsAdd") = True

            ' Enable controls
            txtID.Enabled = True
            cmbSup.Enabled = True
            txtNom.Enabled = True
            txtManf.Enabled = True
            txtModel.Enabled = True
            txtUPri.Enabled = True
            cmbUIss.Enabled = True

            ' Setup Buttons
            cmdAdd.Enabled = False
            cmdAdd.ImageUrl = "oel2_btn_add_disabled.bmp"
            cmdEdit.Enabled = False
            cmdEdit.ImageUrl = "oel2_btn_edit_disabled.bmp"
            cmdSave.Enabled = True
            cmdSave.ImageUrl = "oel2_btn_save.bmp"
            cmdCancel.Enabled = True
            cmdCancel.ImageUrl = "oel2_btn_cancel.bmp"
            cmdDelete.Enabled = False
            cmdDelete.ImageUrl = "oel2_btn_delete_disabled.bmp"
            cmdMainMenu.Enabled = False
            cmdMainMenu.ImageUrl = "oel2_btn_mainmenu_disabled.bmp"

            ' Clear controls
            txtID.Text = ""
            txtNom.Text = ""
            txtManf.Text = ""
            txtModel.Text = ""
            txtUPri.Text = "0.00"
            cmbSup.SelectedIndex = 0
            cmbUIss.SelectedValue = "EA"

        End Sub

        Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdEdit.Click
            ' Enable controls
            txtID.Enabled = True
            cmbSup.Enabled = True
            txtNom.Enabled = True
            txtManf.Enabled = True
            txtModel.Enabled = True
            txtUPri.Enabled = True
            cmbUIss.Enabled = True

            ' Setup Buttons
            cmdAdd.Enabled = False
            cmdAdd.ImageUrl = "oel2_btn_add_disabled.bmp"
            cmdEdit.Enabled = False
            cmdEdit.ImageUrl = "oel2_btn_edit_disabled.bmp"
            cmdSave.Enabled = True
            cmdSave.ImageUrl = "oel2_btn_save.bmp"
            cmdCancel.Enabled = True
            cmdCancel.ImageUrl = "oel2_btn_cancel.bmp"
            cmdDelete.Enabled = False
            cmdDelete.ImageUrl = "oel2_btn_delete_disabled.bmp"
            cmdMainMenu.Enabled = False
            cmdMainMenu.ImageUrl = "oel2_btn_mainmenu_disabled.bmp"

        End Sub

        Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdCancel.Click
            Dim selIndex As Integer = -1

            ' Enable controls
            txtID.Enabled = False
            cmbSup.Enabled = False
            txtNom.Enabled = False
            txtManf.Enabled = False
            txtModel.Enabled = False
            txtUPri.Enabled = False
            cmbUIss.Enabled = False

            ' Setup Buttons
            cmdAdd.Enabled = True
            cmdAdd.ImageUrl = "oel2_btn_add.bmp"
            cmdEdit.Enabled = True
            cmdEdit.ImageUrl = "oel2_btn_edit.bmp"
            cmdSave.Enabled = False
            cmdSave.ImageUrl = "oel2_btn_save_disabled.bmp"
            cmdCancel.Enabled = False
            cmdCancel.ImageUrl = "oel2_btn_cancel_disabled.bmp"
            cmdDelete.Enabled = True
            cmdDelete.ImageUrl = "oel2_btn_delete.bmp"
            cmdMainMenu.Enabled = True
            cmdMainMenu.ImageUrl = "oel2_btn_mainmenu.bmp"

            ' Fill controls back up
            ' =====================
            ' Find selIndex
            If cmbSearchBy.SelectedValue = "Nomenclature" Then
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductName") Then
                        selIndex = x
                        Exit For
                    End If
                Next
            Else
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductCode") Then
                        selIndex = x
                        Exit For
                    End If
                Next
            End If
            ' Fill controls
            txtID.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductCode")
            txtNom.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductName")
            txtManf.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Manufacturer")
            txtModel.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Model")
            txtUPri.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("UnitPrice")
            cmbUIss.SelectedValue = DS_DBItem1.Tables("products").Rows(selIndex).Item("Qty_Issue")

            ' Determine supplier from ID code
            Dim supIndex As Integer = -1
            For x As Integer = 0 To DS_DBItem1.Tables("suppliers").Rows.Count - 1
                If DS_DBItem1.Tables("suppliers").Rows(x).Item("SupplierID") = DS_DBItem1.Tables("products").Rows(selIndex).Item("SupplierCode") Then
                    supIndex = x
                    Exit For
                End If
            Next
            cmbSup.SelectedValue = DS_DBItem1.Tables("Suppliers").Rows(supIndex).Item("CompanyName")

        End Sub

        Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdSave.Click
            Dim selIndex As Integer = -1
            Dim OSupplier As String

            ' Find Supplier Code
            For d As Integer = 0 To DS_DBItem1.Tables("Suppliers").Rows.Count - 1
                If DS_DBItem1.Tables("suppliers").Rows(d).Item("CompanyName") = cmbSup.SelectedValue.ToString Then
                    OSupplier = DS_DBItem1.Tables("suppliers").Rows(d).Item("SupplierID")
                End If
            Next

            ' Find selIndex
            If cmbSearchBy.SelectedValue = "Nomenclature" Then
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductName") Then
                        selIndex = x
                        Exit For
                    End If
                Next
            Else
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductCode") Then
                        selIndex = x
                        Exit For
                    End If
                Next
            End If


            If Session("IsAdd") = False Then ' Edit
                Dim dr As DataRow
                dr = DS_DBItem1.Tables("products").Rows(selIndex)
                dr.Item("ProductCode") = txtID.Text
                dr.Item("SupplierCode") = OSupplier.ToString
                dr.Item("ProductName") = txtNom.Text
                dr.Item("UnitPrice") = txtUPri.Text
                dr.Item("Manufacturer") = txtManf.Text
                dr.Item("Model") = txtModel.Text
                dr.Item("Qty_Issue") = cmbUIss.SelectedValue.ToString
                dr.Item("UnitsInStock") = 0
                dr.Item("ReorderLevel") = 0

                Try
                    ' When you call the update ADO.net will go through all the record that have a rowstate  'Modified' and will update that record
                    SqlDataAdapter3.Update(DS_DBItem1)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                DS_DBItem1.AcceptChanges()
            Else ' Add
                Dim dr As DataRow
                dr = DS_DBItem1.Tables("Products").NewRow
                dr.Item("ProductCode") = txtID.Text
                dr.Item("SupplierCode") = OSupplier.ToString
                dr.Item("ProductName") = txtNom.Text
                dr.Item("UnitPrice") = txtUPri.Text
                dr.Item("Manufacturer") = txtManf.Text
                dr.Item("Model") = txtModel.Text
                dr.Item("Qty_Issue") = cmbUIss.SelectedValue.ToString
                dr.Item("UnitsInStock") = 0
                dr.Item("ReorderLevel") = 0
                dr.EndEdit()

                DS_DBItem1.Tables("Products").Rows.Add(dr)

                Try
                    ' When you call the update ADO.net will go through all the record that have a rowstate  'Modified' and will update that record
                    SqlDataAdapter3.Update(DS_DBItem1)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                DS_DBItem1.AcceptChanges()
            End If

            ' Enable controls
            txtID.Enabled = False
            cmbSup.Enabled = False
            txtNom.Enabled = False
            txtManf.Enabled = False
            txtModel.Enabled = False
            txtUPri.Enabled = False
            cmbUIss.Enabled = False

            ' Setup Buttons
            cmdAdd.Enabled = True
            cmdAdd.ImageUrl = "oel2_btn_add.bmp"
            cmdEdit.Enabled = True
            cmdEdit.ImageUrl = "oel2_btn_edit.bmp"
            cmdSave.Enabled = False
            cmdSave.ImageUrl = "oel2_btn_save_disabled.bmp"
            cmdCancel.Enabled = False
            cmdCancel.ImageUrl = "oel2_btn_cancel_disabled.bmp"
            cmdDelete.Enabled = True
            cmdDelete.ImageUrl = "oel2_btn_delete.bmp"
            cmdMainMenu.Enabled = True
            cmdMainMenu.ImageUrl = "oel2_btn_mainmenu.bmp"

        End Sub

        Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdDelete.Click
            Dim selIndex As Integer = -1

            ' Are you sure you want to delete a record?



            ' Find selIndex
            If cmbSearchBy.SelectedValue = "Nomenclature" Then
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductName") Then
                        selIndex = x
                        Exit For
                    End If
                Next
            Else
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    If cmbSearch.SelectedValue = DS_DBItem1.Tables("products").Rows(x).Item("ProductCode") Then
                        selIndex = x
                        Exit For
                    End If
                Next
            End If

            Dim dr As DataRow
            dr = DS_DBItem1.Tables("Products").Rows(selIndex)
            DS_DBItem1.Tables("Products").Rows(selIndex).Delete()
            dr.EndEdit()

            Try
                ' When you call the update ADO.net will go through all the record that have a rowstate  'Modified' and will update that record
                SqlDataAdapter3.Update(DS_DBItem1)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            DS_DBItem1.AcceptChanges()

            selIndex = selIndex - 1
            If selIndex < 0 Then selIndex = 0

            ' Fill controls
            ' =============
            ' Fill controls
            selIndex = 0
            txtID.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductCode")
            txtNom.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductName")
            txtManf.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Manufacturer")
            txtModel.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Model")
            txtUPri.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("UnitPrice")
            cmbUIss.SelectedValue = DS_DBItem1.Tables("products").Rows(selIndex).Item("Qty_Issue")

            ' Repop search box, removing old item, and selecting new item.
            ' Fill in the search combos
            cmbSearch.Items.Clear()
            If cmbSearchBy.SelectedValue = "Nomenclature" Then
                lblSearch.Text = "Nomenclature"
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    cmbSearch.Items.Add(DS_DBItem1.Tables("products").Rows(x).Item("ProductName"))
                Next
            Else
                lblSearch.Text = "Item ID"
                For x As Integer = 0 To DS_DBItem1.Tables("products").Rows.Count - 1
                    cmbSearch.Items.Add(DS_DBItem1.Tables("products").Rows(x).Item("ProductCode"))
                Next
            End If

            cmbSearch.SelectedIndex = selIndex

            ' Fill controls
            txtID.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductCode")
            txtNom.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("ProductName")
            txtManf.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Manufacturer")
            txtModel.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("Model")
            txtUPri.Text = DS_DBItem1.Tables("products").Rows(selIndex).Item("UnitPrice")
            cmbUIss.SelectedValue = DS_DBItem1.Tables("products").Rows(selIndex).Item("Qty_Issue")

            ' Determine supplier from ID code
            Dim supIndex As Integer = -1
            For x As Integer = 0 To DS_DBItem1.Tables("suppliers").Rows.Count - 1
                If DS_DBItem1.Tables("suppliers").Rows(x).Item("SupplierID") = DS_DBItem1.Tables("products").Rows(selIndex).Item("SupplierCode") Then
                    supIndex = x
                    Exit For
                End If
            Next
            cmbSup.SelectedValue = DS_DBItem1.Tables("Suppliers").Rows(supIndex).Item("CompanyName")


    JP
    0
     

    Author Comment

    by:gleznov
    I've actually cut out all the old code (posted above) and started again, and this time it works like this (same result though)

    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 new 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

            SqlDataAdapter1.Fill(DS_Suppliers1)
            SqlDataAdapter2.Fill(DS_Units1)
            SqlDataAdapter3.Fill(DS_Products1)

            cmbSearch1.DataBind()
            cmbSearch2.DataBind()
            cmbSup.DataBind()
            cmbUIss.DataBind()

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

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

                cmbUIss.SelectedValue = "EA"

                FillControls()

            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
            Else
                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
            Next

            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?

    JP
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    Try writing:

    cmbUIss.SelectedIndex = selIndex

    instead of:

    cmbUIss.SelectedValue = DS_Products1.Tables(0).Rows(selIndex).Item("qty_issue")
    0
     

    Author Comment

    by:gleznov
    cmbUIss is unit issue - it draws from a different table than everything else.  This one works fine - it's the rest that doesn't.  

    Another thing I'm having trouble with:
    On postback, whatever I choose in cmbSearch1 immediately reverts to index 0.  I don't know how to stop this.

    JP
    0
     

    Author Comment

    by:gleznov
    Oops, nevermind on the problem in that last post, I had to shift the Dataset fills and the databinds into the if not (ispostback) section.

    JP
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    You mean by the rest this code?

            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")
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    What is cmbSup?

    a ComboBox or ListBox

    I tried doing this:

    selIndex = 0
    txtID.Text = ds.Tables(0).Rows(selIndex).Item("CustomerID")

    and it works.

    i want to try it with cmbSup, but need to know the type of control.
    0
     
    LVL 8

    Accepted Solution

    by:
    I think i know what is causing your problem:

    are you sure that the value return by DS_Products1.Tables(0).Rows(selIndex).Item("SupplierCode") exists in the control  cmbSup?

    If not you will get the error:

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

     cmbSup.SelectedValue = DS_Products1.Tables(0).Rows(selIndex).Item("SupplierCode")

    instead of that try

      Dim _index As Integer
      Dim _Item As New ListItem(DS_Products1.Tables(0).Rows(selIndex).Item("SupplierCode"))
      _Item.Value = _Item.Text
       _index = Me.cmbSup.Items.IndexOf(_Item)
        Me.cmbSup.SelectedIndex = _index
    0
     

    Author Comment

    by:gleznov
    cmbsup is a dropdownlist - I'm used to calling them combo boxes from windows apps.

    Also, cmbSup contains the company name, the value is the suppliercode - I'll go look at that right away.  Thanks!

    I've found a few other minor errors in the code and am still debugging at it - I think something to do with several smaller errors is producing the larger error...  will get back in a bit...


    JP
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    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…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    884 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

    17 Experts available now in Live!

    Get 1:1 Help Now