Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to convert DataSet to ArrayList and ArrayList to DataSet in ASP.NET

Author: Nikki Smith
Download Source Code :

In this article, I will show you how you can convert DataSet to ArrayList and ArrayList to DataSet in ASP.NET


First I will convert DataSet to ArrayList and then I will convert ArrayList to DataSet. You can download source code from above.


  1. Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site. No Need to change name of the Page
  3. Add a GridView control to Web Site

    <asp:GridView ID="GridView1" runat="server">

  4. Namespaces used in this code
    Visual Basic

    Imports System.Collections
    Imports System.Data
    Imports System.Data.SqlClient
    Visual C#

    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
  5. Write below function to convert DataSet to ArrayList
    This function will convert DataSet to ArrayList. I have created a Table with the name of ‘Countries’ in ‘master’ database for this article. You need to set connection to your Server and you can get data from any database. I have created a DataSet object and filled this object with SqlDataAdapter object by getting data from my database.
    The last part of the function is the important part. I have created a new ArrayList object and used a foreach loop to loop throw each row of table of my DataSet. In foreach loop, I have added each row to ArrayList.
    Visual Basic
    Private Function ConvertDataSetToArrayList() As ArrayList
            Dim conn As New SqlConnection("Data Source=YourServer;Initial Catalog=master;Integrated Security=True")
            Dim cmd As New SqlCommand("Select Country From Countries", conn)
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            Dim arrlst As New ArrayList()
            For Each row As DataRow In ds.Tables(0).Rows
            Return arrlst
        End Function
    Visual C#
    private ArrayList ConvertDataSetToArrayList()
        SqlConnection conn = new SqlConnection("Data Source=YourServer;Initial Catalog=master;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("Select Country From Countries", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ArrayList arrlst = new ArrayList();
        foreach (DataRow row in ds.Tables[0].Rows)
        return arrlst;
  6. Write below function to convert ArrayList to DataSet
    This function will convert ArrayList to DataSet. I have created a ArrayList object and added five items to it. Then DataSet and DataTable objects are created and DataTable is added to DataSet.
    Then I have added a column to table with the name of ‘Country’ and set ‘string’ as its data type.
    Now comes the part of code where we convert our ArrayList to DataSet. Here again I have used a foreach loop to loop throw each string in ArrayList object. In foreach loop, I have created a new row of table in DataSet for every value of ArrayList and added this row to table of DataSet.
    Visual Basic
    Private Function ConvertArrayListToDataSet() As DataSet
        Dim arrlst As New ArrayList()
        Dim ds As New DataSet()
        Dim dt As New DataTable()
        ds.Tables(0).Columns.Add("Country", System.Type.[GetType]("System.String"))
        For Each str As String In arrlst
            If str <> String.Empty Then
                Dim row As DataRow = ds.Tables(0).NewRow()
                row(0) = str
            End If
        Return ds
    End Function
    Visual C#
    private DataSet ConvertArrayListToDataSet()
        ArrayList arrlst = new ArrayList();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        ds.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
        foreach (string str in arrlst)
            if (str != string.Empty)
                DataRow row = ds.Tables[0].NewRow();
                row[0] = str;
        return ds;
  7. Write below Page Load method

    Visual Basic
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        GridView1.DataSource = ConvertArrayListToDataSet()
    End Sub
    Visual C#
    protected void Page_Load(object sender, EventArgs e)
        GridView1.DataSource = ConvertArrayListToDataSet();
  8. Now you can see Web Site in your browser
Add Article Comment:
Name :
Email Address :
Comments :
<< How to perform SQL Server Transaction in ASP.NET

Disclaimer - Privacy
© 2002-2017