You question seems nothing to do with ArrayList to me. What exactly the error you have seen You may want to debug it by print out the result from DataReader like: While oServerReader.Read ' try print out oServerReader.GetString(0) End While
Thanks!
JohnChen
rtnArray is an arraylist I've dimmed. Now I want to add each line from the reader to it, so that what I pass back is an arraylist where each item is a row from the reader.
I'll then want to know how to access the various entries in each item on the arraylist.
well I think the point paul was making is, why are you using an arraylist when a datatable is available to you.
why rebuild the wheel
now if you are absolutely adamant about returning an ArrayList where each item in the ArrayList is represents a row from the reader as an array of values, the easiest way of doing it is. . .
dim sql as string = "select . . . " dim da as SQLDataAdapter = new SQLDataAdapter(sql, someConnection) dim dt as datatable = new DataTable() da.Fill(dt) foreach row as DataRow in dt.Rows rtnArray.Add(row.ItemArray) next row
but again, that would be foolish. just use the datatable. Why copy data from a perfectly adequate structure to an inadequate structure
is it because you need to pass the rtnArray to another method that takes an array list as an argument
Well, that's the design error!!! Don't declare your methods with Class type parameters, declare them with interfaces!!!
The method should not take an ArrayList, but an interface - ICollection or IList.
for example, Never declare this method:
sub DoSomething(byval MyList as ArrayList)
It is plain wrong!!! Declare this method:
sub DoSomething(byval MyList as IList)
Fill a Dataset, and pass the DefaultView to the method or if you want, pass an ArrayList or any other class that implements ICollection/IList.
note: my vb may have typos as I rarely use the inferior language
I think you could solve this if you have a temporary storage for each of your row in the datareader. You can achieve this by creating a class/struct that corresponds to the fields in your row:
Structure test Public id As Integer Public name As String End Structure
'' On your read loop while oServerReader.Read Dim row As test row.id = oServerReader("Id") Arraylist.add(row) End While
BTW, you can use a class instead of a structure...
You question seems nothing to do with ArrayList to me. What exactly the error you have seen You may want to debug it by print out the result from DataReader like: While oServerReader.Read ' try print out oServerReader.GetString(0) End While
dim sql as string = "select . . . " dim da as SQLDataAdapter = new SQLDataAdapter(sql, someConnection) dim dt as datatable = new DataTable() da.Fill(dt) foreach row as DataRow in dt.Rows rtnArray.Add(row.ItemArray) next row
could also be done as:
While rdr.Read Dim arr(rdr.FieldCount) As Object rdr.GetValues(arr) rtnArray.Add(arr) End While
Hi Interesting dialog. I think the reason why Dave wants to populate it into array is because he cant do a UNION for a list of data read from the database.
How do you make a UNION from the data read from a cursor You cant. So populate it into array and later display it a table.
You can just create an adapter and fill a datatable. I think this approach is much convenient. Coz its quite the same as traversing the whole reader and placing it in an array list...
Thanks, but I'm looking to fill an arraylist, not a datatable.
You can just create an adapter and fill a datatable. I think this approach is much convenient. Coz its quite the same as traversing the whole reader and placing it in an array list...
I hope the following code will be of great help to you
While oServerReader.Read Dim Tmp As System.Collections.ArrayList = New System.Collections.ArrayList Dim lintCnt As Integer = 0 While lintCnt < oServerReader.FieldCount Tmp.Add(oServerReader(0)) System.Math.Min(System.Threading.Interlocked.Increment(lintCnt),lintCnt-1) End While rtnArray.Add(Tmp) End While
Arraylist from SQLReader
Castro
rtnArray is an arraylist I've dimmed. Now I want to add each line from the reader to it, so that what I pass back is an arraylist where each item is a row from the reader.
I'll then want to know how to access the various entries in each item on the arraylist.
Noorali
why rebuild the wheel
now if you are absolutely adamant about returning an ArrayList where each item in the ArrayList is represents a row from the reader as an array of values, the easiest way of doing it is. . .
dim sql as string = "select . . . "
dim da as SQLDataAdapter = new SQLDataAdapter(sql, someConnection)
dim dt as datatable = new DataTable()
da.Fill(dt)
foreach row as DataRow in dt.Rows
rtnArray.Add(row.ItemArray)
next row
but again, that would be foolish. just use the datatable.
Why copy data from a perfectly adequate structure to an inadequate structure
is it because you need to pass the rtnArray to another method that takes an array list as an argument
Well, that's the design error!!!
Don't declare your methods with Class type parameters, declare them with interfaces!!!
The method should not take an ArrayList, but an interface - ICollection or IList.
for example, Never declare this method:
sub DoSomething(byval MyList as ArrayList)
It is plain wrong!!!
Declare this method:
sub DoSomething(byval MyList as IList)
Fill a Dataset, and pass the DefaultView to the method or if you want, pass an ArrayList or any other class that implements ICollection/IList.
note: my vb may have typos as I rarely use the inferior language
OCCcoach
but the answer later became more and more complicated!
Been called many things. . . rarely get that one
What am I looking for
Get a recipe for Kung-pao Chicken
Your picture Yeah, you are a cutie ;)
Um whats with the copyrighted ADO book pdf on your site, eh
Barrie A Kenyon
It seems that 'beating someone over the head in admonishment' is the real use for the hammer. You didn't need to be quite so verbose.
remster
Hi,
I think you could solve this if you have a temporary storage for each of your row in the datareader. You can achieve this by creating a class/struct that corresponds to the fields in your row:
Structure test
Public id As Integer
Public name As String
End Structure
'' On your read loop
while oServerReader.Read
Dim row As test
row.id = oServerReader("Id")
Arraylist.add(row)
End While
BTW, you can use a class instead of a structure...
cheers,
Paul June A. Domag
zeeshan_ahmad
You question seems nothing to do with ArrayList to me.
What exactly the error you have seen
You may want to debug it by print out the result from DataReader like:
While oServerReader.Read
' try print out oServerReader.GetString(0)
End While
Thanks!
JohnChen
LonW
dim sql as string = "select . . . "
dim da as SQLDataAdapter = new SQLDataAdapter(sql, someConnection)
dim dt as datatable = new DataTable()
da.Fill(dt)
foreach row as DataRow in dt.Rows
rtnArray.Add(row.ItemArray)
next row
could also be done as:
While rdr.Read
Dim arr(rdr.FieldCount) As Object
rdr.GetValues(arr)
rtnArray.Add(arr)
End While
Paulyz
How do you make a UNION from the data read from a cursor You cant. So populate it into array and later display it a table.
Do you have an idea how
roadragedsb
Thanks, but I'm looking to fill an arraylist, not a datatable.
AndresF
Hi,
You can just create an adapter and fill a datatable. I think this approach is much convenient. Coz its quite the same as traversing the whole reader and placing it in an array list...
cheers,
Paul June A. Domag
Washington
While oServerReader.Read
Dim Tmp As System.Collections.ArrayList = New System.Collections.ArrayList
Dim lintCnt As Integer = 0
While lintCnt < oServerReader.FieldCount
Tmp.Add(oServerReader(0))
System.Math.Min(System.Threading.Interlocked.Increment(lintCnt),lintCnt-1)
End While
rtnArray.Add(Tmp)
End While
Arno Nel
The question orignal seemed easy,
but the answer later became more and more complicated!
But you are all professional,
so want more information
Here comes it for you, see my sig...
May you like it!
Sunil Pawar
Master Carpenter: "Don't drive screws with a hammer, use a screwdriver!"
Apprentice Carpenter: "But Master, I have a hammer in my hand so I have to use the hammer!"
Master Carpenter: "Well, put it down and pick up a screwdriver!"
Apprentice Carpenter: "But Master, I only know how to use hammers!"
Master Carpenter: "Well, obviously you don't because you are trying to drive screws with it!"
Apprentice Carpenter: "Master, you are a bad master, because you won't train me to drive screws with a hammer!"
Master Carpenter: "I think you should find another line of work as you lack the apptitude and attidude neccesary to become a master carpenter."
kittensattack