Click here to Skip to main content
16,021,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI I know the simplest way to convert to object array using datatable. All i want it to do in another function which will dynamically convert to object. Something like this---
C#
public class clsBindObjectUsingDataTable
    {
        private int intCount;
        private List<object> objList;
        public void BindObject(object[] objMyObjectList, DataTable dttable)
        {
            objList = new List<object>(typeof(objMyObjectList));//to get property //of the object 
            objList = objMyObjectList.ToList();
            foreach (DataRow drRow in dttable.Rows)
            {
                intCount = drRow .ItemArray.Length;
                for (int i = 0; i < intCount;i++ )
                {
                    objList[0][i].Add(drRow.ItemArray[i]); ///i know this doesnt work 
//just wanted to give my idea
                }
                //objList.Add(drRow.ItemArray);                
            }
            objMyObjectList = objList.ToArray();
        }
    }
Posted
Updated 24-Sep-11 19:32pm
v2

1 solution

Look at this article Using Reflection to convert DataRows to objects or objects to DataRows

You might want to try this extension method also :

C#
ObjectArray[] objectArray = DataSet.ToSpecifiedObject<ObjectArray[]>();

public static T ToSpecifiedObject<T>(this DataSet value)
{
    byte[] buf = System.Text.UTF8Encoding.UTF8.GetBytes(value.GetXml());
    MemoryStream ms = new MemoryStream(buf);
    XmlSerializer ser = new XmlSerializer(typeof(T));
    object obj = ser.Deserialize(ms);

    return (T)obj;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900