Solved

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

Posted on 2004-10-28
175 Views
Last Modified: 2007-12-19
OK:

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

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

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

        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
Question by:gleznov
    2 Comments
     
    LVL 4

    Accepted Solution

    by:
    Hi,

    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
            Else
                Control.Items.Add(Code)
                Control.SelectedValue = Code
            End If
        End Sub

    Customize it at your own will

    Glom
    0
     

    Author Comment

    by:gleznov
    Can you give me a little more info on how to use that in my program?  

    JP
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    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!

    Suggested Solutions

    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 …
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    856 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

    13 Experts available now in Live!

    Get 1:1 Help Now