Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox selected item and Label.Text

Status
Not open for further replies.

Alexandrumbm

Programmer
Jun 20, 2007
13
RO
Hello again,

I have this code an it's working perfect for loading data from my database but...

I have some labels... and i want to populating them when i select an combobox item... with data from the selected row.

How can i do this ?!



---------------
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\..\..\data\fedt.mdb;Jet OLEDB:Database Password=***************")
Dim cmd As OleDb.OleDbCommand
Dim sql = "SELECT nume, cnp, act_identitate FROM nomenclator_clienti_pf"
Dim objRead As Object

conn.Open()

cmd = New OleDb.OleDbCommand(sql, conn)
objRead = cmd.ExecuteReader()
While objRead.read()
Me.cmb_cumparator_pf.Items.Add(objRead("nume"))
End While

objRead.close()

Me.lbl_cnp_cumparator.Text = Me.cmb_cumparator_pf.SelectedItem

Me.cmb_cumparator_pf.Text = FixNull(cmd.ExecuteScalar)

If conn.State <> ConnectionState.Closed Then
conn.Close()
End If

Me.cmb_cumparator_pf.Text = ""
Me.cmb_cumparator_pf.Focus()

End If
-------------------------

i want to display in the labels the data from the selected row from database..

when i select a name from combobox to show me some info into the labels, info that is stored in the database too.

Sorry for my bad english and thanks in advice!
 
Hello,

Create a DataSet with a DataTable to hold your data and then you can bind like this:
Code:
' Put this in form level variables.
Private m_DataSet As New DataSet

' Get rid of all of this code.
' Dim objRead As Object
' conn.Open()
' cmd = New OleDb.OleDbCommand(sql, conn)
' objRead = cmd.ExecuteReader()
' While objRead.read()
'     Me.cmb_cumparator_pf.Items.Add(objRead("nume"))
' End While
' objRead.close()

' Put this in your code instead.
' There is no need to open and close the connection as the DataAdapter
' will use it and leave it in the state in which it was found.
cmd = New OleDb.OleDbCommand(sql, conn)
Dim dataAdapter As New OleDb.OleDbDataAdapter(cmd)
dataAdapter.Fill(m_DataSet, "YourTableName") 

Me.YourComboBox.DataSource = m_DataSet.Tables("YourTableName")

Me.YourLabel1.DataBindings.Add("Text", Me.YourComboBox, "YourTableColumnName1")
Me.YourLabel2.DataBindings.Add("Text", Me.YourComboBox, "YourTableColumnName2")

' Me.lblCompanyName.DataBindings.Add("Text", Me.cboCustomers, "CompanyName")
 
I get this error...

Cannot bind to the property or column cnp on the DataSource.
Parameter name: dataMember
 
Is the DataSet and DataTable being created. The property name is case sensitive. Can you post the code you are using?
 
Can i do this thru an SQL statement on the event of the combobox value change ?!

To read the selected value from the combobox and display me the nex data from the row ?

 
Hello,

You could do that but it would be pretty inefficient because you would be repopulating your dataset every time instead of once.

Sorry, you will also need to set the DisplayMember property and possibly also the ValueMember property. DisplayMember is what is displayed in the ComboBox while the ValueMember is what is used for lookups, etc. For example, DisplayMember = "CustomerName" and ValueMember = "CompanyID" would display the CustomerName but SelectedItem would contain the corresponding CompanyID which is the primary key.

1. Is the DataSet being populated
2. Is the DataTable being populated
3. After setting DisplayMember, is the correct value being selected and displayed when the ComboBox is dropped

Good Luck!
 
Thanks again SBendBuckeye.

I'll retry all that you tell me.
I will anounce you when it's done, i am a beginner but i think i'll do it.

 
Good, I'm glad it worked. This code may be slightly better, I'm not sure:
Code:
Me.YourLabel1.DataBindings.Add("Text", Me.YourComboBox.DataSource, "YourTableColumnName1")
Me.YourLabel2.DataBindings.Add("Text", Me.YourComboBox.DataSource, "YourTableColumnName2")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top