Trying to fill up an arraylist with data coming from multiple columns
Hi I'm Trying to fill up an arraylist with data coming from multiple columns . How would I do this ?
heres my code :
Dim myConnection As SqlConnection
myConnection = New SqlConnection("Data Source=SqlServer;Initial Catalog=OnlineJobs;Integrated Security=SSPI;")
Dim mycommand As SqlCommand
mycommand = New SqlCommand("SELECT Department,Title,JobCode FROM AvailableJobs", myConnection)
myConnection.Open()
Dim myDataReader As SqlDataReader
Dim departments As New ArrayList
myDataReader = mycommand.ExecuteReader
While myDataReader.Read
departments.Add(myDataReader("Department"))
End While
myDataReader.Close()
myConnection.Close()
Return departments
as you can see on the line thats bold , where it says "Department" , that to my understanding by experience is the column's name you are pulling data from and I cant add multiple column names to the .Add function . I have to use an Arraylist because I'm trying to fill up a ListBox . Thanks
nevermind , figured it out thanks anyways
[1218 byte] By [
Matrix.net] at [2007-11-11 10:00:14]

# 2 Re: Trying to fill up an arraylist with data coming from multiple columns
What was the solution?
you have to create seperate sqlcommands and arraylists for each column you want data from .pretty much what I did uptop but after the code executes for each column , you have to then do a loop that appends each value of each arraylist to a String Variable and then take that string variable and store it in another Arraylist , then clear the string variable at the end of the loop for the next trip around the loop and after you've gone through all the allocations of the arrayLists you then return the arraylist that was storing the stringvariable.
example:
Dim myConnection As SqlConnection
myConnection = New SqlConnection("Data Source=SqlServer;Initial Catalog=OnlineJobs;Integrated Security=SSPI;")
Dim mycommand As SqlCommand
mycommand = New SqlCommand("SELECT Department FROM AvailableJobs", myConnection)
myConnection.Open()
Dim myDataReader As SqlDataReader
Dim departments As New ArrayList
myDataReader = mycommand.ExecuteReader
While myDataReader.Read
departments.Add(myDataReader("Department"))
End While
myconnection.close()
Dim my2ndcommand As SqlCommand
my2ndcommand = New SqlCommand("SELECT Title FROM AvailableJobs", myConnection)
myConnection.Open()
Dim my2ndDataReader As SqlDataReader
Dim titles As New ArrayList
my2ndDataReader = my2ndcommand.ExecuteReader
While my2ndDataReader.Read
titles.Add(myDataReader("Title"))
End While
my2ndDataReader.Close()
myConnection.Close()
'creating now the part of the code that will combine the two previous 'arraylists
dim finalArraylist as new Arraylist
dim i as integer
dim str as string
while i < my2ndArraylist.count
str = departments(i).tostring & " " & Titles(i).tostring
finalArraylist.add(str)
i = i +1
end while
return finalArraylist