I am facing casting problem from array to datarow. in the following function alRows[(int)i] is a system.array. when i run the following function i am getting "specified cast is not valid" error in the dt.Rows.Add((System.Data.DataRow)alRows[(int)i]); line. can anybody pls give me the solution.
thanks in advance
protected DataTable CreateDataTable(string szTableName, string[] vaCols, ArrayList alRows)
{
long i;
DataTable dt = new DataTable(szTableName);
try
dt.Columns.Add(vaCols, Type.GetType("System.String"));
}
dt.Rows.Add((System.Data.DataRow)alRows[(
HandleError(ex, C_CLASS_NAME,
Thankssai
You must bear in mind that Type Casting is only successful, if the source and destination types are of compatible kind.
You have to check how your ArrayList was constructed and if the content of the ArrayList is possible to cast to DataRow type.
In my opinion, most likely, you have to re-design your code.
DataTable.Rows collection has various overloaded Add methods. One is which allows you to pass in array of objects (DataTable.Rows.Add(object[] dataitem)).
DataRow has no public constructor, so most likely that the content of your ArrayList is not likely to be of DataRow instances, and casting these object into DataRow would not be successful.
Maung Maung