Convert dataset to arraylist in asp.net using c#
DataSet is a disconnected, cached set of records that are retrieved from the database. When a connection is established with the database, the DataAdapter creates a DataSet and stores data in it. After the data is retrieved and stored in a DataSet, the connection with the database is closed. Such a working architecture is called disconnected architecture. The DataSet acts like a virtual database containing tables, rows and columns. An application works with the database records stored in the DataSet.
Dataset stores datatables in the Tables collection and we can retrieve a datatable by passing the index of table. So, to convert DataSet to ArrayList, first we need to get the correct datatable in the DataTable collection and take each row and add to the arrayList. The below code demonstrates how to convert data from dataset to an arraylist
public partial class _Default : System.Web.UI.Page { string connectionString = "Data Source=LocalHost; Initial CataLog=ERPFinAccounting; User ID=kris; Password=XXX"; protected void Page_Load(object sender, EventArgs e) { ArrayList myArrayList = ConvertDataSetToArrayList(); // Display each item of ArrayList foreach (Object row in myArrayList) { Response.Write(((DataRow)row)["AccountCode"].ToString() + " " + ((DataRow)row)["AccountName"].ToString() + "<br/>"); } } }
The below code performs the following tasks.
- Instantiate the Connection object
- Instantiate the Command object
- Open the connection
- Instantiate DataAdapter object and Set DataAdapter command properties
- Instantiate DataSet object and Populate the Dataset
- Create an instance of ArrayList
- To retrieve each DataRow of DataTable which is stored in the DataSet we have used the foreach loop. Inside the foreach loop we add each DataRow object to an ArrayList. We pass DataRow object to the Add method of an ArrayList as the parameter.
public ArrayList ConvertDataSetToArrayList() { SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("Select * from AccountsTable",conn); cmd.CommandType = CommandType.Text; conn.Open(); SqlDataAdapter myAdapter = new SqlDataAdapter(); myAdapter.SelectCommand = cmd; DataSet myDataSet = new DataSet(); myAdapter.Fill(myDataSet); ArrayList myArrayList = new ArrayList(); foreach (DataRow dtRow in myDataSet.Tables[0].Rows) { myArrayList.Add(dtRow); } conn.Close(); return myArrayList; }
ArrayList Articles
- find item in arraylist using indexof method
- bind arraylist with gridview control in asp net application
- how to bind arraylist with dropdownlist in asp net application
- how to bind arraylist with radiobuttonlist in asp.net application
- convert arraylist to string array
- convert string array to arraylist
- convert datatable into an arraylist
- how to retrieve arraylist items in reverse order
- difference between arraylist and list collection
- Arrays vs ArrayList
- how to create and add items into the ArrayList in csharp
- how to add items to the arraylist using an add method
- retrieve arraylist items using enumerators
- retrieve arraylist items using for each loop
- retrieve arraylist items using indexers
- insert items to the arraylist using insert and insertrange method
- remove items from arraylist
Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls