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
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.