Wednesday, 4 December 2013

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.

The below first method code is very useful for convert DataSet to arraylist. First we need to establish a connection to SQL server. And get data from database and bind data to DatSet after we convert dataset data to arraylist.

// DataSet to arraylist
public ArrayList DatasettoArrayList()
{
DataSet dsDataSet = new DataSet();
ArrayList list=new ArrayList();
using (SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=FirstSample; User ID=sa;Password=sa@123"))
{
using (SqlCommand cmd = new SqlCommand("select * from Employee ", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsDataSet);

foreach (DataRow dtrow in dsDataSet.Tables[0].Rows)
{
list.Add(dtrow);
}
}
}
return list;
}


This below method is used to convert datatable to arraylist

public ArrayList ConvertDataTabletoArrayList()
{
DataTable dtdatatable = new DataTable();
// List<UserDetails> details = new List<UserDetails>();
ArrayList list = new ArrayList();
using (SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=FirstSample; User ID=sa;Password=sa@123"))
{
using (SqlCommand cmd = new SqlCommand("select * from Employee", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtdatatable);

foreach (DataRow dtrow in dtdatatable.Rows)
{
list.Add(dtrow);
}
}
}
return list;
}

No comments:

Post a Comment