Link to home
Start Free TrialLog in
Avatar of gleznov
gleznov

asked on

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

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
Avatar of jpontani
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
Avatar of gleznov

ASKER

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

JP
Sorry, it should be LBound(listbox.items).  Also, replace 'listbox' with the name of your listbox.
Avatar of gleznov

ASKER

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
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.
Avatar of gleznov

ASKER

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
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?
Avatar of gleznov

ASKER

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
Avatar of gleznov

ASKER

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
Try writing:

cmbUIss.SelectedIndex = selIndex

instead of:

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

ASKER

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
Avatar of gleznov

ASKER

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
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")
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.
ASKER CERTIFIED SOLUTION
Avatar of wguerram
wguerram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gleznov

ASKER

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