Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Combo Box ValueMember/Selected Value

Posted on 2010-11-09
8
Medium Priority
?
529 Views
Last Modified: 2012-05-10
With EE help I have managed to get a working Combo Box that loads sales reps using the code attached.

I want to capture the UserID (which is the ValueMember) in the combo box as a long integer so I can use it for filter a list view. But with what I have so far, my cboSalesRep.ValueMember is null. So I am missing something.

THANKS FOR ANY HELP.
Private Sub cboSalesRep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSalesRep.SelectedIndexChanged
        'Set the Public variable to the Combo Box Value
        lngSelectedSalesRepID = CLng(Me.cboSalesRep.SelectedValue)
    End Sub

Open in new window

0
Comment
Question by:Buck_Beasom
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:TheMozz
ID: 34094718
Try using the Items property along with the selected index property:

lngSelectedSalesRepID = CLng(Me.cboSalesRep.Items[Me.cboSalesRep.SelectedIndex].Value)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34094989
SelectedValue will only work with DataBinding. You need to cast the SelectedItem to your custom type and retrieve the ID from it.
0
 

Author Comment

by:Buck_Beasom
ID: 34095121
The Mozz plan throws an error. VB doesn't seem to like that syntax.

Carl, you've gotten me this far (and I thought I'd give you a break!) I don't know how to code "cast the SelectedItem to your custom type". I'm WAY new at this.

Thanks.
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34095248
Something like:

      Dim item As ListItem = CType(cboSalesRep.SelectedItem, ListItem)
      Dim id As Integer = item.ID
0
 
LVL 8

Expert Comment

by:TheMozz
ID: 34095272
Does it help to add a call to ToString() at the end of your code:

lngSelectedSalesRepID = CLng(Me.cboSalesRep.SelectedValue.ToString)

Ref: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_23230044.html
0
 

Author Comment

by:Buck_Beasom
ID: 34095488
Carl:

I put the code in the cboSalesRep_SelectedIndexChanged event.

The comma after SelectedItem throws an error.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 34095630
Saying what?
0
 

Author Closing Comment

by:Buck_Beasom
ID: 34095822
Saying "YOU DA MAN."

I tried to combine two steps into 1, so instead of using CType the way you had it, and then setting the lngSelectedRepID variable to the resulting id value, I tried using Clng.

When I did it this way:
Private Sub cboSalesRep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSalesRep.SelectedIndexChanged
        'Set the Public variable to the Combo Box Value
        Dim item As ListItem = CType(cboSalesRep.SelectedItem, ListItem)
        Dim lngItemID As Long = item.ID

        lngSelectedSalesRepID = lngItemID
    End Sub

It worked like a charm!

Thanks again!!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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