[RESOLVED] [2005] What's wrong with my combo?
This is how I'm binding my combobox:
Code:
Public Sub carregaPais()
Me.ComboPais.DataSource = AbreDataSet("Select * from Paises", "Pais")
Me.ComboPais.DisplayMember = "Pais"
Me.ComboPais.ValueMember = "IDPais"
End Sub
Function AbreDataSet(ByVal strSQL As String, ByVal Tabela As String) As DataSet
Dim ds As DataSet
Dim DataAdapt As Odbc.OdbcDataAdapter
Dim conn As New Odbc.OdbcConnection(sConnString)
DataAdapt = New Odbc.OdbcDataAdapter(strSQL, sConnString)
ds = New DataSet()
conn.Open()
DataAdapt.Fill(ds, Tabela)
Return ds
ds = Nothing
End Function
My combo returns this error:
Cannot bind to the new value member.
Parameter name: value
What am I doing wrong?
Thank you.
Re: [2005] What's wrong with my combo?
You're binding your ComboBox to a DataSet and that DataSet has no property named "IDPais". Nowhere are you telling the ComboBox that it's supposed to get its data from the DataTable named "Pais".
In fact, your DataSet is completely pointless. Gneerally a DataSet with one DataTable in it is useless That's like creating an array to store a single string, except even more inefficient.
If all you want is one DataTable then all you need to create is one DataTable:
VB.NET Code:
Public Sub carregaPais()
Me.ComboPais.DataSource = AbreDataTable("Select * from Paises", "Pais")
Me.ComboPais.DisplayMember = "Pais"
Me.ComboPais.ValueMember = "IDPais"
End Sub
Function AbreDataTable(ByVal strSQL As String, ByVal Tabela As String) As DataTable
Dim conn As New Odbc.OdbcConnection(sConnString)
Dim comm As New Odbc.OdbcCommand(strSQL, conn)
conn.Open()
Dim rdr As Odbc.OdbcDataReader = comm.ExecuteReader()
Dim tbl As New DataTable(Tabela)
tbl.Load(rdr)
rdr.Close()
conn.Close()
Return tbl
End Function
Re: [2005] What's wrong with my combo?
Hi jmcilhinney.
Thank you for the reply but steel have the same error.
What should do next?
Thank you.
Re: [2005] What's wrong with my combo?
If you've followed my suggestion and it still doesn't work then your DataTable mustn't have a column named "IDPais". Note that both DisplayMember and ValueMember are case-sensitive. An invalid DisplayMember will simply be ignored but an invalid ValueMember will always throw an exception.
Re: [2005] What's wrong with my combo?
Ok. It was the case sensitive!
Now it's working.
Thank you once again.