473,320 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

help with listbox from class array

Hey all, I have a question for you all. I'm learning vb.net and need some
help. I have two classes one named Customers and one named CustomersDA. I
have to go though Customer for everything. I have a listbox that needs to
get data from an array. I call a function called GetAll that is a ArrayList
in Customers that then calls CustomersDA.GetAll that is also an array list
but returns customers (see code below).I need to fill a listbox from the
ArrayList from GetAll. I cant seem to get it to work. Here are the classes
and code:
Imports System.Data.OleDb

Imports System.Collections

Public Class CustomerDA

Shared customers As New ArrayList() ' Customer references

Shared aCustomer As Customer

'Declare a connection. Eliminate the hard-coded path to

'the database by putting the database file in the

'project's Bin folder

Shared cnnCustomer As New _

OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & _

"Data Source=Customers.mdb")

'Declare variables for Customer attribute values

Shared name, address, phoneno As String

'Initialize Method (chapter 13 example)

'no connection passed as reference

Public Shared Sub Initialize()

Try

' Try to open the connection

cnnCustomer.Open()

Catch e As Exception

Console.WriteLine(e.ToString)

End Try

End Sub

' Terminate Method (chapter 13 example)

Public Shared Sub Terminate()

Try

cnnCustomer.Close()

cnnCustomer = Nothing

Catch e As Exception

Console.WriteLine(e.Message.ToString)

End Try

End Sub

' AddNew Method --Throws DuplicateException if exists

Public Shared Sub AddNew(ByVal aCustomer As Customer)

' Get customer information

name = aCustomer.GetName

address = aCustomer.GetAddress

phoneno = aCustomer.GetPhoneNo

' Declare a string SQL statement

Dim sqlInsert As String = "INSERT INTO customerTable " & _

"VALUES ('" & name & "', '" & address & "', '" & phoneno & " ')"

Dim adpcustomer As New OleDbDataAdapter()

Try

Dim c As Customer = Find(phoneno)

Throw New DuplicateException(" Customer Exists ")

Catch e As NotFoundException

Try ' Assign Insert Commands and Execute

adpcustomer.InsertCommand = New OleDbCommand(sqlInsert)

adpcustomer.InsertCommand.Connection = cnnCustomer

adpcustomer.InsertCommand.ExecuteNonQuery()

Finally

End Try

End Try

End Sub

' Find Method--Throws NotFoundException if Not Found

Public Shared Function Find(ByVal key As String) As Customer

Dim acustomer As New Customer()

acustomer = Nothing

Dim dsCustomer As New DataSet()

Try

' Define the SQL SQL statement using the phoneno key

Dim sqlQuery As String = "SELECT Name, Address, PhoneNO " & _

"FROM CustomerTable WHERE phoneNo = '" & key & "'"

Dim adpCustomer As New _

OleDbDataAdapter(sqlQuery, cnnCustomer)

adpCustomer.Fill(dsCustomer, "CustTable")

If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim custRow As DataRow

custRow = dsCustomer.Tables("custTable").Rows(0)

name = custRow.Item("Name")

address = custRow.Item("address")

phoneno = custRow.Item("phoneno")

acustomer = New Customer(name, address, phoneno)

Else

Throw New NotFoundException("Not Found")

End If

dsCustomer = Nothing

Catch e As OleDb.OleDbException

Console.WriteLine(e.Message.ToString)

End Try

Return acustomer

End Function

' GetAll Method

Public Shared Function GetAll() As ArrayList

Dim dsCustomer As New DataSet()

Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _

"FROM CustomerTable"

Try

Dim adpCustomer As New _

OleDbDataAdapter(sqlQuery, cnnCustomer)

adpCustomer.Fill(dsCustomer, "CustTable")

If dsCustomer.Tables("CustTable").Rows.Count > 0 Then

Dim dsRow As DataRow

' Clear the array list

customers.Clear()

For Each dsRow In dsCustomer.Tables("CustTable").Rows

name = dsRow("Name")

address = dsRow("Address")

phoneno = dsRow("PhoneNo")

Dim aCustomer As New _

Customer(name, address, phoneno)

customers.Add(aCustomer)

Next

Else

' No records in database

End If

dsCustomer = Nothing

Catch e As Exception

Console.WriteLine(e.ToString)

Throw New NotFoundException("Not Found")

End Try

Return customers

End Function


Nov 21 '05 #1
2 1502
Allen,

Can you tell why your instructor tells you this crazy thing.

I see not the sense at all. It would be crazy if everybody was doing it as
you are ordered.

This is in my opinion the same as making a program that multiply without
using the multiply operator.

(Or it should be a strictly scolar operation of course, which it is the best
you communicate with your instructor then).

Just my thought,

Cor
Nov 21 '05 #2
I agree but that is what I have to do. So how do I get it done?

allen
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Allen,

Can you tell why your instructor tells you this crazy thing.

I see not the sense at all. It would be crazy if everybody was doing it as
you are ordered.

This is in my opinion the same as making a program that multiply without
using the multiply operator.

(Or it should be a strictly scolar operation of course, which it is the
best
you communicate with your instructor then).

Just my thought,

Cor

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
2
by: Goober | last post by:
I have the following default.aspx page that works properly. However, what I want to do is to link the graphics within it (that are hard coded now in the default web page) to our corporate...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
1
by: A. Spiehler | last post by:
I'm trying to fill a listBox control with string members from an array of objects. I think using data binding is supposed to be the easiest way to do this. I've never used data binding before and...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
6
by: JOSII | last post by:
Getting a string of boolean value into and out of the registry is no problem. Here's the problem: Although you can place an object into the registry and retreive it, I need to place an ArrayList...
0
by: raypjr | last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the...
2
by: raypjr | last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the...
15
by: kronecker | last post by:
Is it possible to address a listbox in a general manner depending on it's number eg ListBox1. ListBox2. etc
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.