Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cannot understand why its not giving me the out put. There is data in the described table and even the dataset is getting populated I saw that in debugger mode. But, the value member and display member are not getting populated. I cannot understand why.

What I have tried:

Private Sub frmItemEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSql As String
        strSql = "Select UID, UnitName from UnitofMeasurement"
        Try
            conn.Open()
            With com
                .CommandType = CommandType.Text
                .CommandText = strSql
                .Connection = conn
            End With
            adp.SelectCommand = com
            adp.Fill(ds)
            adp.Dispose()
            com.Dispose()
            conn.Close()
            With cmbUnitOfMeasure
                .DataSource = ds.Tables("UnitofMeasurement")
                .ValueMember = "UID"
                .DisplayMember = "UnitName"
            End With
            
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        conn.Close()
    End Sub
Posted
Updated 16-Sep-16 0:59am
v3
Comments
Richard MacCutchan 15-Sep-16 8:08am    
You have two different spellings: UnitofMeasurement and UnitOfMeasurement.
Member 12741660 15-Sep-16 8:32am    
Made the correction no use. the name of table is UnitofMeasurement.

You cannot use a DataTable as the DataSource for a ComboBox, see ComboBox.DataSource Property (System.Windows.Forms)[^].

Karthik showed me the error of my way(s). :(
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 17-Sep-16 7:56am    
5 for your humbleness.
try this
VB
cmbUnitOfMeasure.ValueMember = "UID"
cmbUnitOfMeasure.DisplayMember = "UnitName"
cmbUnitOfMeasure.DataSource = ds.Tables(0)
 
Share this answer
 
Comments
Richard MacCutchan 15-Sep-16 12:03pm    
That is the same as OP already has coded, but ComboBox does not accept a DataTable as DataSource.
Karthik_Mahalingam 15-Sep-16 13:49pm    
HI Richard
it will work, i did that many times.
refer this : http://stackoverflow.com/a/12495086

the difference in my code is declare the valuemember and displaymember property first and then assign the datasource to it..
and also, ds.Tables(0) , since [ ds.Tables("UnitofMeasurement") ] this code is not convincing...

Richard MacCutchan 15-Sep-16 15:00pm    
If you already did it then you should post the full code. I tried a very simple example and it did not work.
Karthik_Mahalingam 15-Sep-16 15:26pm    
Check this.

Dim dt As DataTable = New DataTable
dt.Columns.Add("UID")
dt.Columns.Add("UnitName")
dt.Rows.Add(1, "a")
dt.Rows.Add(2, "b")
dt.Rows.Add(3, "c")
dt.Rows.Add(4, "d")

cmbUnitOfMeasure.DataSource = dt
cmbUnitOfMeasure.ValueMember = "UID"
cmbUnitOfMeasure.DisplayMember = "UnitName"
Richard MacCutchan 16-Sep-16 5:12am    
Thanks for the reply, I just tried the following:

Dim table1 As DataTable = New DataTable("UnitOfMeasurement")
table1.Columns.Add("UID")
table1.Columns.Add("UnitName")
table1.Rows.Add(1, "Inch")
table1.Rows.Add(2, "Foot")
table1.Rows.Add(3, "Yard")
table1.Rows.Add(4, "Furlong")
' Create a DataSet. Put table in it.
Dim ds As DataSet = New DataSet("Length")
ds.Tables.Add(table1)
' Visualize DataSet.
With ComboBox1
.DataSource = ds.Tables("UnitOfMeasurement")
.ValueMember = "UID"
.DisplayMember = "UnitName"
End With

which worked first time. I cannot understand how it did not work before, when my code was (AFAIR) the same.

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