Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Convert datatable/dataset to arraylist in asp.net

Apr 1, 2012
Introduction:

In this article I will explain how to convert datatable or dataset data to arraylist using asp.net.


Description:
  
I explained many articles relating to Asp.net, C#.NET, Gridview, JQuery etc. During working with those articles I came across one situation that is converting datatable to arraylist and dataset to arraylist to use it in applications. During that time I realized it would helpful for others if I write post on this. Please check the below methods to convert datatable to arraylist and dataset to arraylist in asp.net. 

C#.NET Code


/// <summary>
/// This Method is used to convert dataset object to arraylist
/// </summary>
/// <returns></returns>
public ArrayList ConvertDatasettoArrayList()
{
DataSet ds = new DataSet();
// List<UserDetails> details = new List<UserDetails>();
ArrayList list=new ArrayList();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true"))
{
using (SqlCommand cmd = new SqlCommand("select TOP 10 UserId,UserName,Location from UserInformation", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

foreach (DataRow dtrow in ds.Tables[0].Rows)
{
list.Add(dtrow);
}
}
}
return list;
}
/// <summary>
/// This method is used to convert datatable to arraylist
/// </summary>
/// <returns></returns>
public ArrayList ConvertDataTabletoArrayList()
{
DataTable dt = new DataTable();
// List<UserDetails> details = new List<UserDetails>();
ArrayList list = new ArrayList();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true"))
{
using (SqlCommand cmd = new SqlCommand("select TOP 10 UserId,UserName,Location from UserInformation", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

foreach (DataRow dtrow in dt.Rows)
{
list.Add(dtrow);
}
}
}
return list;
}
VB.NET Code:


''' <summary>
''' This Method is used to convert dataset object to arraylist
''' </summary>
''' <returns></returns>
Public Function ConvertDatasettoArrayList() As ArrayList
Dim ds As New DataSet()
' List<UserDetails> details = new List<UserDetails>();
Dim list As New ArrayList()
Using con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true")
Using cmd As New SqlCommand("select TOP 10 UserId,UserName,Location from UserInformation", con)
con.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds)

For Each dtrow As DataRow In ds.Tables(0).Rows
list.Add(dtrow)
Next
End Using
End Using
Return list
End Function
''' <summary>
''' This method is used to convert datatable to arraylist
''' </summary>
''' <returns></returns>
Public Function ConvertDataTabletoArrayList() As ArrayList
Dim dt As New DataTable()
' List<UserDetails> details = new List<UserDetails>();
Dim list As New ArrayList()
Using con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true")
Using cmd As New SqlCommand("select TOP 10 UserId,UserName,Location from UserInformation", con)
con.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)

For Each dtrow As DataRow In dt.Rows
list.Add(dtrow)
Next
End Using
End Using
Return list
End Function

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

birkin said...

I got a software to convert this date, It's easy!

Anonymous said...

Please provide the snapshot image :Convert datatable/dataset to arraylist in asp.net
Input:Data Table
Output:Arraylist
Both Arraylist Structure will be same as Data Table Format....................................plz solve this problem soon.............

Anonymous said...

I am getting this result System.Data.DataRow
System.Data.DataRow

Anonymous said...

Please help to resolve this

Anonymous said...

ArrayList list = new ArrayList();
SqlDataAdapter da = new SqlDataAdapter("Select * from customer", con);
DataTable dt = new DataTable();
da.Fill(dt);
Response.Write(dt);

foreach (DataRow dtrow in dt.Rows)
{
list.Add(dtrow);
}
for (int i = 0; i < list.Count - 1; i++)
{
Response.Write(list[i] + "
");
}

Anonymous said...

Ignore response.write(dt);

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.