Thursday, September 11, 2008

DataSet,DataTable,Dataview & ArrayList Essentials

C# code for ASP.Net Convert DataSet to ArrayList


// SQL Select Command
SqlCommand mySqlSelect = new SqlCommand("select * from categories", mySQLconnection);mySqlSelect.CommandType = CommandType.Text;
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);


// 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() + "
");
}





C# Code for ASP.Net Convert DataSet to DataView


// SQL Select Command
SqlCommand mySqlSelect = new SqlCommand("select * from categories", mySQLconnection);
mySqlSelect.CommandType = CommandType.Text;

SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);

DataSet myDataSet = new DataSet();

mySqlAdapter.Fill(myDataSet);



// Convert DataSet to DataView using DefaultView property associated with DataTable stored inside the DataSet
DataView dView = myDataSet.Tables[0].DefaultView;

You can bind the initialized object of DataView class with ASP.Net data presentation control such as GridVew, DataList or repeater to display the data on ASP.Net web page.
E.g.:
GridView1.DataSource = dView;
GridView1.DataBind();

C# Code for ASP.Net Convert ArrayList to Array


// ArrayList class object

ArrayList arrlist = new ArrayList();
// add items to arrlist collection using Add method

arrlist.Add("item 1");

arrlist.Add("item 2");

arrlist.Add("item 3");

arrlist.Add("item 4");

arrlist.Add("item 5");
// ToArray function: converts the ArrayList collection to Array type

Array myArray = arrlist.ToArray(typeof(string));
for (int i = 0; i <>

{

Response.Write(myArray.GetValue(i) + "
");

}
Response.Write("
");

arrlist.Clear();

arrlist.Add(1);

arrlist.Add(2);

arrlist.Add(3);

arrlist.Add(4);

arrlist.Add(5);
// ToArray function: converts the ArrayList collection to Array type

myArray = arrlist.ToArray(typeof(int));
for (int i = 0; i <>

{

Response.Write(myArray.GetValue(i) + "
");

}

You can also convert the ArrayList collection to array type using ToArray function without passing the System.Type parameter, this will convert the ArrayList collection to object[] array type. In the above C# sample code arrlist.Clear() method is used to remove all the items stored in the ArrayList collection to reuse it for adding integer type items to understand the System.Type parameter passed to the ToArray function.