In ASP.Net, to convert DataSet to ArrayList using C# code requires some knowledge about DataSet: in-memory data collection. You must know that ASP.Net DataSet can hold the schema, DataTable as well as data retrieved from the SQL database table. DataSet represents the in-memory structure of table data retrieved from the database. DataSet as the name suggests consists of DataTable collection that can store the table structure i.e. column names, table name. It stores each DataTable in the collection at zero- based index that can be retrieved by passing the index of table whose data you want to manipulate, display and bind to any data presentation control of ASP.Net. For converting the ASP.Net DataSet to ArrayList you can select the DataTable based on its index in the DataTable Collection and insert its each row into the ArrayList as an object.
C# code for ASP.Net Convert DataSet to ArrayList
DataSet myDataSet = new DataSet();
// create an instance for ArrayList
ArrayList myArrayList = new ArrayList();
// foreach loop to read each DataRow of DataTable stored inside the DataSet
foreach (DataRow dRow in myDataSet.Tables[0].Rows)
{
// add DataRow object to ArrayList
myArrayList.Add(dRow);
}
Above C# code shows how to convert DataSet to ArrayList. Add function of ArrayList accepts the parameter as object type. DataRow object has been passed in the above example to the Add function of ArrayList to store it in the collection.
Now the next step is also necessary to retrieve the values of columns stored in each row added to the ArrayList collection. Following C# code shows how to retrieve the ArrayList item as object, then convert it to DataRow and read the column value to display it on the ASP.Net web page:
// foreach loop to get each item of ArrayList
foreach (Object objRow in myArrayList)
{
Response.Write(((DataRow)objRow)[ "categoryId" ].ToString() + " " + ((DataRow)objRow)[ "categoryName" ].ToString() + "
");
}
Wednesday, January 20, 2010
Subscribe to:
Comments (Atom)

