Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2008, 05:00 PM
Authorized User
 
Join Date: Jan 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert datareader into ArrayList

Hi
I am trying to convert the datreader into ArrayList. The ArrayList consist of four columns But what i am all getting is All the data in one column with multiple rows. What i actually what is to store all the data in ArrayList with the same format as it appeared in datareader so that i can bound it with GridView.

My current code is given bellow:

Code:
   Dim dr As SqlDataReader = cmd.ExecuteReader()
        Dim al As New ArrayList

        If (dr.HasRows) Then

            While (dr.Read())

                Dim fields As Integer = dr.FieldCount()
                ' Dim a As Integer

                For fields = 0 To dr.FieldCount - 1
                    al.Add(dr.Item(fields))
                Next
            End While
        End If
 
Old February 25th, 2008, 05:06 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

O.o First Peter has given you a good jumping off point in this post:

http://p2p.wrox.com/topic.asp?TOPIC_ID=69286

Second, why are you going about binding a GridView like this?

Third, what do you expect is going to happen based on your code? You simply loop all of the fields in your datareader and add the values of a given field to your arraylist...which is 1 Dimensional.

A better approach would be to use a DataSet or a DataTable.

http://www.google.com/search?hl=en&r...Using+DataSets

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 26th, 2008, 10:48 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

SKhna,

First, as Doug pointed out an ArrayList is 1 dimension. If you want to put more than 1 bit of data into it for each item, you need to put in something which itself has the several bits of data (like a class instance).

If you are dealing with tabular data, use a tabular object like the DataReader or DataTable.

You can bind a DataReader directly to a control, so there's no reason to convert or copy the data.

-Peter
peterlanoie.blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert arraylist into array mohitbhutani C# 2 June 14th, 2011 04:00 AM
datareader to arraylist SKhna ASP.NET 2.0 Basics 3 February 25th, 2008 11:03 AM
read datareader and convert binary to string Maxxim ASP.NET 1.0 and 1.1 Professional 3 July 9th, 2007 07:14 PM
Help with an arraylist crazy-nun General .NET 4 July 14th, 2005 03:32 AM
Convert xml to an arraylist manu1076 XML 0 May 19th, 2004 07:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.