Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have this subroutine to fill some comboboxes

Private Sub Fill_Combo_Box(ByVal ParmSQL As String, ByVal ComboName As ComboBox)
    Dim Con As OleDbConnection
    Dim sqlCmd As OleDbCommand = New OleDbCommand(ParmSQL)
    Con = New OleDbConnection(Main_Form.ConString)
    Try
        Con.Open()
        sqlCmd.Connection = Con
        myData = sqlCmd.ExecuteReader
        Do While myData.Read
            ComboName.ValueMember = myData.Item(0)
            ComboName.Items.Add(myData.Item(1).ToString.PadRight(10, " ") & " " & myData.Item(2))
        Loop
    Catch 'ex As OledbException
    End Try
    Con.Close()
End Sub


The subroutine work great but please help me how i can take at any time the
value from the selecteditem??

Thanks and regards...
Posted

If you just want the text of the Item that was selected, you can use

<br />
ComboName.SelectedItem.Text;<br />


if you want the actual value and not the text:

<br />
ComboName.SelectedValue;<br />


You should get the values on the SelectedIndexChanged Event of your comboBox.
I hope this is what you wanted to know. Good Luck my friend
 
Share this answer
 
Comments
Sandeep Mewara 15-Apr-11 13:38pm    
Comment from OP:
I want to take the ComboName.ValueMember = myData.Item(0)
when i perform the write record subroutine


the ComboName.SelectedValue return nothing..............
mario_silent 15-Apr-11 13:43pm    
OK I can see a problem here... You are adding the data from the reader dinamycally and when I set the valueMember property is usually when I bind to a dataSource. Maybe it would be a good Idea to assign the datasource of the combo to the reader instead of filling it dynamically and after adding the datasource and assigning the valueMember, you can loop through the items of the list to change the text displayed in the combo to your format
Vagelisr 15-Apr-11 15:09pm    
I am really appreciate if you write some code for this.....

Thank you......
Ok, first you should use OleDbAdapter and a dataset instead of the reader. Your code would look like this:

VB
Private Sub Fill_Combo_Box(ByVal ParmSQL As String, ByVal ComboName As ComboBox)    Dim Con As OleDbConnection
Dim adapter as OleDbAdapter    
Dim dataSet as DataSet
Dim sqlCmd As OleDbCommand = New OleDbCommand(ParmSQL)    
Con = New OleDbConnection(Main_Form.ConString)    

Try        
Con.Open()        
sqlCmd.Connection = Con        
adapter.SelectCommand = sqlCmd
adapter.Fill(dataSet)

'Then we set the dataSource to the dataset's table       
ComboName.DataSource = dataSet.Tables(0)
ComboName.ValueMember = "NameOfFirstColumn"
ComboName.DisplayMember = ("NameOfSecondColumn").PadRight(10, " ") & " " & "NameOfThirdColumn"

           
Catch 'ex As OledbException    End Try    Con.Close()End Sub


I'm not too sure about the displayMember being a valid expression, I'm guessing not but maybe you could add a new column to your sql table that has the text in the format you want and then you'd just say:

ComboName.DisplayMember = ("NameOfNewColumnWithTheFormat")

Meanwhile you can test just with the valuemember and not mess with the displayMember by using SelectedItem.Value. Good Luck
 
Share this answer
 
Comments
Vagelisr 15-Apr-11 15:51pm    
I receive an error
Type OleDbAdapter is not define
mario_silent 15-Apr-11 15:55pm    
My bad. It's OleDbDataAdapter and I forgot to initialize the Dataset and the adapter as well. Do so or you'll get some error
Vagelisr 15-Apr-11 16:21pm    
My friend first of all thank you for your time.
With this 2 lines
ComboName.ValueMember = "NameOfFirstColumn"ComboName.DisplayMember = ("NameOfSecondColumn").PadRight(10, " ") & " " & "NameOfThirdColumn"
i see my id in my combo box.....
i want to see the second and the thierd column and receive the first........
mario_silent 15-Apr-11 16:29pm    
Have you thought about what I suggested? adding a fourth column that combines column 2 and 3 and gives it the format you want? then:

ComboName.DisplayMember = "NameOfFourthColumn"

That's a possibility but also you could use a List<>, populate it and bind it to the comboBox. The drawback here would be that you need to create a new class or struct with 2 properties, one for the ID and the other could be the combination of the column 2 and three. So you bind the List<yourclass struct=""> to the combo and set the DisplayMember to the name of the Property containing the column 2 and 3. I can show you how to do it if you like this option or you can try adding the column to your sql table but it might not be a good choice for you. You're welcome1
!
Vagelisr 15-Apr-11 16:36pm    
I have try to change the sql in this
"SELECT line_id, Left(line_code & ' ', 11) as code, line_descr FROM Line Order by line_code"

but :(


If you are able to show me it will be very helpfully.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900