How to make array of object and to add them to listbox?

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
Imports db = System.Data

Public Class CustomerADO
Dim cid As Integer
Dim fn, ln, em, pw As String
Dim bl As Decimal



Private ssConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\QADEER\Documents\MyShop.accdb"

Public Sub GetCustomers()

Dim dbConn As db.OleDb.OleDbConnection
dbConn = New db.OleDb.OleDbConnection(ssConnection)

dbConn.Open()

Dim sql As String = "Select * from Customer;"

Dim dbCmd As db.OleDb.OleDbCommand
dbCmd = New db.OleDb.OleDbCommand(sql, dbConn)


Dim DataReader As db.OleDb.OleDbDataReader
DataReader = dbCmd.ExecuteReader

Do While DataReader.Read


cid = System.Convert.ToInt32(DataReader("CID"))
fn = System.Convert.ToString(DataReader("FirstName"))
ln = System.Convert.ToString(DataReader("LastName"))
em = System.Convert.ToString(DataReader("EmailAddress"))
bl = System.Convert.ToDecimal(DataReader("Balance"))
pw = System.Convert.ToString(DataReader("Password"))

Dim objCustomer As Customer
objCustomer = New Customer(cid, fn, ln, em, bl, pw)



Loop
End Sub

End Class

what should I do in the while loop to make array of customers objects.
&
what should be the code in the form to add this array of customers to listbox.

If this information is not enough plz ask for additional information.Thanks.:D
 
Look in to List (of T) class. It is basically a dynamic Array. That means at any time you can add and remove items in the Array.

You could use it by declaring the list before your while loop.

VB.NET:
Dim customerList as new List (of Customer)

Then in each iteration of your while loop invoke the .Add Method to add a customer to the list. Ill let you figure that code out.

You can bind any List to a dropdownlist, so once you finish adding all your customers you can easily bind the dropdownlist to the customerList.
 
Thanks

Now that I have created a list of customer what would be code for adding it to the listbox do I have use a loop because by adding customerlist to listbox it shows only one item objectCollection.
plz tell me the code for loop.
 
Back
Top