SgDotNet
Singapore Professional .NET User Group -For Cool Developers

C# casting problem from array to datarow

Latest post 01-18-2006 3:10 PM by MaungMaung. 1 replies.
  • 01-18-2006 2:38 PM

    C# casting problem from array to datarow

     Greetings All,

    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

    {

    for (i = 0; i <= Information.UBound(vaCols, 1); i++)

    {

    dt.Columns.Add(vaColsIdea [I], Type.GetType("System.String"));

    }

    if (! Information.IsNothing(alRows))

    {

    if (alRows.Count > 0)

    {

    for (i = 0; i <= alRows.Count - 1; i++)

    {

    dt.Rows.Add((System.Data.DataRow)alRows[(int)i]);

    }

    }

    }

    }

    catch (Exception ex)

    {

    HandleError(ex, C_CLASS_NAME,false);

    }

    return dt;

    }

     

    Thanks
    sai

  • 01-18-2006 3:01 PM In reply to

    Re: C# casting problem from array to datarow

    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

     

    Maung Maung
Page 1 of 1 (2 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems