none
Convert Data Table to List of Objects RRS feed

  • Question

  • Having a issue with my code. No errors but its not building the object and adding it to the list. It just adding objects with empty/null values. What am I doing wrong?

    public List<WBGProduct> ConvertDataTableToWBGProductList(DataTable dt)
            {
                List<WBGProduct> lst = new List<WBGProduct>();
    
                try
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        WBGProduct Product = new WBGProduct();
    
                        foreach (DataColumn col in dt.Columns)
                        {                       
                            if (row[col.ColumnName] == "ID") { Product.ID = Convert.ToInt64(row[col].ToString()); }
                            if (row[col.ColumnName] == "Product_Name") { Product.Product_Name = row[col].ToString(); }
                            if (row[col.ColumnName] == "Available") { Product.Display = Convert.ToBoolean(row[col].ToString()); }
                            if (row[col.ColumnName] == "Image_Name") { Product.Image_Name = row[col].ToString(); }
                            if (row[col.ColumnName] == "Image_Data") { Product.Image = Encoding.ASCII.GetBytes(row[col].ToString()); }
                            if (row[col.ColumnName] == "Release_Date") { Product.Release_Date = Convert.ToDateTime(row[col].ToString()); }
                            if (row[col.ColumnName] == "Description") { Product.Description = row[col].ToString(); }
    
                            
                        }
    
                        lst.Add(Product);
                    }
    
                    return lst;
                }
                catch (Exception ex)
                {
                    return lst;
                }
            }

    Monday, January 14, 2019 9:19 PM

Answers

  • Seems something like this would be worth trying which only works when property names match DataColumn names,

    private static List<T> ConvertDataTable<T>(DataTable dt)  
    {  
        List<T> data = new List<T>();  
        foreach (DataRow row in dt.Rows)  
        {  
            T item = GetItem<T>(row);  
            data.Add(item);  
        }  
        return data;  
    }  
    private static T GetItem<T>(DataRow dr)  
    {  
        Type temp = typeof(T);  
        T obj = Activator.CreateInstance<T>();  
      
        foreach (DataColumn column in dr.Table.Columns)  
        {  
            foreach (PropertyInfo pro in temp.GetProperties())  
            {  
                if (pro.Name == column.ColumnName)  
                    pro.SetValue(obj, dr[column.ColumnName], null);  
                else  
                    continue;  
            }  
        }  
        return obj;  
    }  

    Usage

    var lst = ConvertDataTable<WBGProduct>(dt); 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 14, 2019 9:33 PM
    Moderator

All replies

  • Seems something like this would be worth trying which only works when property names match DataColumn names,

    private static List<T> ConvertDataTable<T>(DataTable dt)  
    {  
        List<T> data = new List<T>();  
        foreach (DataRow row in dt.Rows)  
        {  
            T item = GetItem<T>(row);  
            data.Add(item);  
        }  
        return data;  
    }  
    private static T GetItem<T>(DataRow dr)  
    {  
        Type temp = typeof(T);  
        T obj = Activator.CreateInstance<T>();  
      
        foreach (DataColumn column in dr.Table.Columns)  
        {  
            foreach (PropertyInfo pro in temp.GetProperties())  
            {  
                if (pro.Name == column.ColumnName)  
                    pro.SetValue(obj, dr[column.ColumnName], null);  
                else  
                    continue;  
            }  
        }  
        return obj;  
    }  

    Usage

    var lst = ConvertDataTable<WBGProduct>(dt); 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 14, 2019 9:33 PM
    Moderator
  • Maybe you should replace ‘if (row[col.ColumnName] == "ID")’ with ‘if (col.ColumnName == "ID")’ or use a single loop like this:

    foreach (DataRow row in dt.Rows)

    {

       WBGProduct Product = new WBGProduct();

       Product.ID = Convert.ToInt64(row["ID"]);

       Product.Product_Name = row["Product_Name"].ToString();

       Product.Display = Convert.ToBoolean(row["Available"]);

       . . .

       lst.Add(Product);

    }

    Monday, January 14, 2019 9:42 PM
  • Why are you using a datatable to begin with? You can just use a datareader and mate the collection of objects without involving a datatable.
    Monday, January 14, 2019 9:48 PM