Skip to content

HELP Loading a Class Property from a DataRow

David Johnson

Asked May 18, 2008, 8:21 AM EDT2 answers
Share
LinkedInX

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

C#
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

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    GOT IT

     

    C#
            private void LoadRow()
            {
                string fieldName;
                int fieldindex = 0;
                string valuetowrite;
                GetColumns();
                propInfoArray = GetType().GetProperties();
                foreach (PropertyInfo propInfo in propInfoArray)
                {
                    try
                    {
                        fieldName = propInfo.Name;
                        DataColumn column = datarow.Table.Columns[fieldName];
                        if (column != null)
                        {
                            fieldindex = datarow.Table.Columns[fieldName].Ordinal;
                            valuetowrite = datarow[fieldindex].ToString();
                            object value = new object();
                            switch (datarow.Table.Columns[fieldName].DataType.FullName)
                            {
                                case "System.String":
                                    {
                                        value = valuetowrite;
                                        break;
                                    }
                                case "System.Int32":
                                    {
                                        value = Int32.Parse(valuetowrite);
                                        break;
                                    }
                                case "System.Decimal":
                                    {
                                        value = decimal.Parse(valuetowrite);
                                        break;
                                    }
                                case "System.Boolean":
                                    {
                                        value = Boolean.Parse(valuetowrite);
                                        break;
                                    }
                                default:
                                    {
                                        break;
                                    }
                            }
                            propInfo.SetValue(this, value, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        string message = ex.Message;
                    }
                }
            }
    0
  • 0
    I wouldlove to ditch the Switch Statement
    0