2
Reply

HELP Loading a Class Property from a DataRow

David Johnson

David Johnson

May 18 2008 12:21 PM
3.9k

I have a project that I am working on A series of classes that inherit a base class

First of all I can loop through the Properties and build a column list.

I am new to Reflection and just do not understand what is wrong

private void GetColumns()
{
      columnlist = new List<TableColumn>();
      propInfoArray = GetType().GetProperties();
      foreach (PropertyInfo propInfo in propInfoArray)
      {
          ColumnAttribute columnattribute = (ColumnAttribute)Attribute.GetCustomAttribute(propInfo, typeof(ColumnAttribute));
          if (columnattribute != null)
          {
             columnlist.Add(new TableColumn(columnattribute.DbColumnName, propInfo.Name, propInfo.PropertyType, columnattribute.DataLoadSavePolicy, columnattribute.SqlType, columnattribute.ColumnFriendlyName, columnattribute.ColumnDescription));
           }
      }
}

I need to loop through a passed in DataRow and populate the values

private void LoadRow(object ObjectToLoad)
{
     try
     {
          GetColumns();
                foreach (TableColumn column in columnlist)
                {
                    string fieldname = column.DbColumnName;
                    FieldInfo fldInfo = ObjectToLoad.GetType().GetField(column.ClassPropertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
                 }
      }
      catch (Exception ex)
      {
            string message = ex.Message;
       }
}

 

[Column(DbColumnName = "ProductPagePath", DataLoadSavePolicy = ColumnAttribute.DataLoadSavePolicyEnum.Both, SqlType = SqlDbType.VarChar)]
public String ProductPagePath
{
     get { return _productPagePath; }
     set
     {
     if (value != _productPagePath)
     {
          HasUnsavedChanges = true;
          _productPagePath = value;
          }
     }
}

When I populate fldInfo it is always null. Here are the calues of the first item

 


ObjectToLoad   
 {ePortal.Core.ProductProfile} 
 object {ePortal.Core.ProductProfile}

column        
 {ePortal.App_Code.TableColumn} 
 ePortal.App_Code.TableColumn

fieldname 
 "ProductPagePath"        
 string

datarow.Table.Columns.List 
 Count = 23 
 System.Collections.ArrayList

datarow.Table.Columns.List.[4] 
 {ProductPagePath} 
 object {System.Data.DataColumn}

datarow.ItemArray[4]
 "/printing/custom-bookmarks.aspx" 
 object {string}

ObjectToLoad.ProductPagePath 
 'object' does not contain a definition for 'ProductPagePath' and no extension method 'ProductPagePath' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) 

((ePortal.Core.ProductProfile)(ObjectToLoad)).ProductPagePath 
 null 
 string


Answers (2)