Introduction:
In this article I will explain how to convert datatable or dataset data to arraylist using asp.net.
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.
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. |
|||
|
|||
6 comments :
I got a software to convert this date, It's easy!
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.............
I am getting this result System.Data.DataRow
System.Data.DataRow
Please help to resolve this
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] + "
");
}
Ignore response.write(dt);
Note: Only a member of this blog may post a comment.