Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Database and Reporting > creating class as datasource


Reply
 
Thread Tools Display Modes
  #1  
Old 12-20-2004, 02:51 AM
Carrie Carrie is offline
Contributor
 
Join Date: Nov 2002
Posts: 485
Default creating class as datasource


I am creating for my data entry forms a class which represents the data on the form (for example I have a personlisting class which will define the fields contained within a person record) - see code below

Code:
Imports System.Data.OleDb Public Class PersonListing Inherits ReadOnlyCollectionBase #Region "Load" Public Sub Load() Dim oCon As New OleDb.OleDbConnection(ConnectionString) Dim oCmd As New OleDb.OleDbCommand With oCmd .Connection = oCon .CommandType = CommandType.Text .CommandText = "Select * from tblPerson" End With oCon.Open() Try Dim oDr As OleDb.OleDbDataReader oDr = oCmd.ExecuteReader While oDr.Read innerlist.Add(New Person(CInt(oDr("ID")), CStr(oDr("Name")), CStr(oDr("Surname")), CStr(oDr("TelNr")))) End While oDr.Close() Catch ex As Exception MsgBox(ex.ToString()) Finally oCon.Close() End Try End Sub

Then I want to display this list in my combo box by doing the following:

Code:
PersonListing.Load With Combobox1 .datasource = PersonListing End with

I am getting an unhandled exception with complex data binding accepts as a data source either an IList or an IList source

Not sure where I go from here?

Last edited by Shurik12; 12-25-2004 at 04:29 PM.
Reply With Quote
  #2  
Old 12-28-2004, 06:03 AM
kreeben kreeben is offline
Freshman
 
Join Date: Oct 2004
Posts: 35
Default

I am not sure what is causing the exceptin. But by just skimming through your code serios doubts come to my mind from this statement:

Code:
.datasource = PersonListing
Your class is of the type PersonListing. If the DataSource should be of the type IList then your class should return an IList object. If you create a property in your class, GetList, which returns an IList, then you should be able to bind the data like this:

Code:
With Combobox1
 .datasource = PersonListing.GetList()
End With
Reply With Quote
  #3  
Old 01-02-2005, 10:23 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

In addition, you shouldn't be assigning the datasource to a public database connection class like so. If you want to have a public connection module thats fine, but you want to assign the datasource of your comboBox to a DataTable, ArrayList, or anything that implements IList. I suggest creating a class for your dataset, and pass the dataset class object to this datasource method.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->