8 Replies - 4942 Views - Last Post: 04 February 2013 - 05:41 PM Rate Topic: -----

#1 kristina1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

get text of the selected item in combobox

Posted 04 February 2013 - 02:38 PM

I have a combobox which is filled from a binding source from the database. i want at some point of the program to get the selected value of the combobox.

The table in the database has a column of Id which is Integer and a column of Description which is string and this is the value that appears at the combobox, so i select as display member the description column and as value member the Id column.

Can i get the description of the selected item on the combobox? Which property should i use, i tried selectedValue and selectedText but neither of them is appropriate for this case.

Thanks a lot for your time
Kristina

Is This A Good Question/Topic? 0
  • +

Replies To: get text of the selected item in combobox

#2 ybadragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 02:43 PM

Try using

Dim description As String = ""
'replace "ComboBox" with your comboboxes name
description = ComboBox.SelectedItem.ToString()


Was This Post Helpful? 0
  • +
  • -

#3 kristina1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 02:53 PM

View Postybadragon, on 04 February 2013 - 02:43 PM, said:

Try using

Dim description As String = ""
'replace "ComboBox" with your comboboxes name
description = ComboBox.SelectedItem.ToString()



i tried your code at selectedIndex_changed event of the combobox and i got this error "Object reference not set to an instance of an object."
Was This Post Helpful? 0
  • +
  • -

#4 ybadragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 03:57 PM

Quote

i tried your code at selectedIndex_changed event of the combobox and i got this error "Object reference not set to an instance of an object."


Can you post your code so we can make sure you are initializing instances of everything?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2891
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 04:00 PM

View Postkristina1, on 04 February 2013 - 02:53 PM, said:

View Postybadragon, on 04 February 2013 - 02:43 PM, said:

Try using

Dim description As String = ""
'replace "ComboBox" with your comboboxes name
description = ComboBox.SelectedItem.ToString()



i tried your code at selectedIndex_changed event of the combobox and i got this error "Object reference not set to an instance of an object."

You did change ComboBox to the name of your control..?
Was This Post Helpful? 0
  • +
  • -

#6 kristina1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 04:03 PM

View Postandrewsw, on 04 February 2013 - 04:00 PM, said:

View Postkristina1, on 04 February 2013 - 02:53 PM, said:

View Postybadragon, on 04 February 2013 - 02:43 PM, said:

Try using

Dim description As String = ""
'replace "ComboBox" with your comboboxes name
description = ComboBox.SelectedItem.ToString()



i tried your code at selectedIndex_changed event of the combobox and i got this error "Object reference not set to an instance of an object."

You did change ComboBox to the name of your control..?


yes,of course
Was This Post Helpful? 0
  • +
  • -

#7 kristina1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 05:24 PM

View Postybadragon, on 04 February 2013 - 03:57 PM, said:

Quote

i tried your code at selectedIndex_changed event of the combobox and i got this error "Object reference not set to an instance of an object."


Can you post your code so we can make sure you are initializing instances of everything?

here is my code:
 Private Sub InstitutionIdComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstitutionIdComboBox.SelectedIndexChanged
       Dim s As String = ""
        s = InstitutionIdComboBox.SelectedValue   

        s = TryCast(InstitutionIdComboBox.SelectedItem, ApInstitution).InstitutionName  
        ........
        
    End Sub


i mean, i tried the method you suggested like this
Dim description As String = ""
description = InstitutionIdComboBox.SelectedItem.ToString()



but i got the same result

This post has been edited by kristina1: 04 February 2013 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2891
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 05:37 PM

You should split the following into separate statements:

s = TryCast(InstitutionIdComboBox.SelectedItem, ApInstitution).InstitutionName 


Dim apInst As ApInstitution = TryCast(InstitutionIdComboBox.SelectedItem, _ 
    ApInstitution)
If apInst Is Nothing Then
    'failed
Else
    s = apInst.InstitutionName
End If

It defeats the purpose of TryCast to attempt to do this in one line, and it will be easier to debug if it is separated.

But you are trying to convert a SelectedItem to an ApInstitution; is this correct..?

This post has been edited by andrewsw: 04 February 2013 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 ybadragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 151
  • View blog
  • Posts: 968
  • Joined: 11-May 12

Re: get text of the selected item in combobox

Posted 04 February 2013 - 05:41 PM

@kristina1

Try setting a Breakpoint in the code you provided at line 3, and check the value of SelectedValue, then step through the code using the F8 key, and check the values of your other variables, to see where exactly it is failing to see an Object reference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1