[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-10-28
2
Medium Priority
?
187 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
Comment
Question by:gleznov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
Glom earned 1500 total points
ID: 12435255
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
ID: 12435573
Can you give me a little more info on how to use that in my program?  

JP
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 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