Combobox Error

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
Dear All,

I have problem with following code :
Private Sub frmProdDataEntry_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Categories As TechManagement.DBComponents.ProductDB = New TechManagement.DBComponents.ProductDB
cboCategory.DataSource = Categories.GetProductCategories
End Sub
When I run the application, following error message appears:-
An unhandled exception of type 'System.Exception' occurred in system.windows.forms.dll
Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource
Thanks a lot in advance
 
Then your problem is on this line...

VB.NET:
cboCategory.DataSource = Categories.GetProductCategories

What does the GetProductCategories method do. For it to work in the instance in which you have used it, it would need to return a Datatable etc.. ( or as the exception says a class that implements IList..)
 
Dear vis781,

Thanks for your reply. This is the code I'm using in Getcategories:-



PublicFunction GetProductCategories() As OleDbDataReader
' Create Instance of Connection and Command Object
' Dim techConnection As OleDbConnection = New OleDbConnection(ConfigurationSettings.AppSettings(strConn))
Dim techConnection As OleDbConnection = New OleDbConnection(strConn)
Dim techCommand As OleDbCommand = New OleDbCommand("techProductCategoryList", techConnection)
' Mark the Command as a SPROC
techCommand.CommandType = CommandType.StoredProcedure
techConnection.Open()
Dim result As OleDbDataReader = techCommand.ExecuteReader(CommandBehavior.CloseConnection)

' Return the datareader result
Return result
EndFunction


Is there any clue to overcome this please?

Thanks
 
You cannot bind a DataReader to a WinForms control, plain and simple. You can in WebForms, but that doesn't help in a WinForms app. Rewrite your GetProductCategories method to return a DataTable instead.
 
Alternatively you could cycle through the reader

dr = cmd.ExecuteReader
While dr.Read()
lstDatabase.Items.Add(dr.GetString(0))
End While
 
Dear All,
I tried following code. But this time I'm getting "System.Data.DataViewManagerListItemTypeDescriptor" in the combobox.
Public Function GetProductCategories() As DataSet
' Dim techConnection As OleDbConnection = New OleDbConnection(strConn)
Dim techAdapter As New OleDbDataAdapter("techProductCategoryList", techConnection)
techAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
techConnection.Open()
Dim TechDataSet As New DataSet
techAdapter.Fill(TechDataSet)
Return TechDataSet
techConnection.Close()
End Function
And Calling procedure as:-

Private Sub frmProdDataEntry_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Categories As TechManagement.DBComponents.ProductDB = New TechManagement.DBComponents.ProductDB
cboCategory.DataSource = Categories.GetProductCategories
End Sub
 
Hi

The problem is that you are returning a DataSet but you are not specifying which table to use from the DataSet. To be honest, if your function is only going to return the contents of one query then you would be much better served using a DataTable as it is more lightweight. For example, your function might look like:
VB.NET:
[COLOR=#000000][COLOR=#0000ff]Public[/COLOR] [COLOR=#0000ff]Function[/COLOR] GetProductCategories() [COLOR=#0000ff]As[/COLOR] DataTable
 
[COLOR=#0000ff]    Dim[/COLOR] techConnection [COLOR=#0000ff]As[/COLOR] OleDbConnection = [COLOR=#0000ff]New[/COLOR] OleDbConnection(strConn)
[COLOR=#0000ff]    Dim[/COLOR] techAdapter [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]New[/COLOR] OleDbDataAdapter([COLOR=#800000]"techProductCategoryList"[/COLOR], techConnection)
 
    techAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
 
[COLOR=#008000]   'NB: No need to open the connection as the DataAdapter will handle this for you.[/COLOR]
[COLOR=#008000]    'techConnection.Open()[/COLOR]
 
[COLOR=#0000ff]    Dim[/COLOR] TechDataTable [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]New[/COLOR] DataTable([COLOR=#800000]"TableName"[/COLOR])
    techAdapter.Fill(TechDataTable)
 
[COLOR=#0000ff]    Return[/COLOR] TechDataTable
 
[COLOR=#008000]    'This line would never have been called as you have already exited the function by using the above return statement.[/COLOR]
[COLOR=#008000]    'techConnection.Close()[/COLOR]
 
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Function[/COLOR]
[/COLOR]
Then in your calling code you will need to specify which fields to use for the DisplayMember and the ValueMember:
VB.NET:
[COLOR=#0000ff][COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] frmProdDataEntry_Load([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Object[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] System.EventArgs) [COLOR=#0000ff]Handles[/COLOR] [COLOR=#0000ff]MyBase[/COLOR].Load
 
[COLOR=#800000]    cboCategory.DataSource = GetProductCategories()[/COLOR]
[COLOR=#800000]    cboCategory.DisplayMember = [COLOR=#800000]"Field to be used for display"[/COLOR]
    cboCategory.ValueMember = [COLOR=#800000]"Usually the field that holds the primary key"[/COLOR]
 
[/COLOR]
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR]
[/COLOR]


HTH
 
Dear djjeavons,

Thanks for your reply. I tried your example, but error appears as attachment.

Is there any clue?...:confused:
 

Attachments

  • Err.jpg
    Err.jpg
    80 KB · Views: 40
Back
Top