0

I am converting from Datatable to List by using Datatable Extensions. Here when i am trying to add a row to a normal List property its working fine. But when i use a list with in a list i am facing problem. i am not able to set the value in property.

 private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
    {
        T item = new T();
        foreach (var property in properties)
        {

            if (property.Name.Equals("ProductSpec"))
            {
                Type t = property.GetType();
                //  i am getting error when i am trying to do this

                property.SetValue(item, ProductSpecIteration(property, row), null);
            }

            if (row.Table.Columns.Contains(property.Name))
            {                   
                property.SetValue(item, row[property.Name] == DBNull.Value?"":row[property.Name], null);
            }

        }
        return item;
    }


 private static ProductSpec ProductSpecIteration(PropertyInfo property, DataRow row)
    {            
        ProductSpec lstProductSpec = new ProductSpec();
        lstProductSpec.product_specs_id = Convert.ToInt64(row["product_specs_id"]);            
        return lstProductSpec;
    }

I am not able to generically do this. But even i am not able to add this in this extensions? Could any one help me out from this situations

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
Bhuvan
  • 419
  • 1
  • 11
  • 25
  • can you post some details on the error? – Cristian Lupascu Dec 28 '12 at 09:02
  • the error is "Object must implement IConvertible" – Bhuvan Dec 28 '12 at 09:04
  • and you get it when you do `Convert.ToInt64(row["product_specs_id"]);`? – Cristian Lupascu Dec 28 '12 at 09:17
  • @Wolf, That should not be the case, because `row["column"]` should return `object` which is convertible to `long`. @Bhuvan, did you try debugging the code, where is the exception occuring? – SWeko Dec 28 '12 at 09:20
  • OK then what is the exact line where you get the error? It is when you're doing `property.SetValue(...)` for properties other than the id? Can you debug and see for which property you get it and what is the value? – Cristian Lupascu Dec 28 '12 at 09:22
  • first thing is i am not able to set the List which is ina property , so i manually hardcoded and assigned the values in a list which is a metyhod ProductSpecIteration. Now when i try to set the actual List to a property i am getting error exactly at this line " property.SetValue(item, ProductSpecIteration(property, row), null); }" . In my debug after the completion of ProductSpecIteration i am facing this error – Bhuvan Dec 28 '12 at 09:25

1 Answers1

1

U r trying to convert the object returned from DataRow.Item[string] property which cannot be set directly.

string value = row[property.Name] == DBNull.Value?"":row[property.Name].ToString();
property.SetValue(item, Convert.ChangeType(value, property.PropertyType), null);

for the ProductSpec u can use,

  ProductSpec spec = ProductSpecIteration(property, row);
  property.SetValue(item, Convert.ChangeType(spec , property.PropertyType), null);

However, it will work fine without changing the type as the type is same as property type

Hope this helps. You can read more about it here You can also read more about IConvertible Interface here

IConvertible interface helps in returning a required value, since object does not implement IConvertible and hence cannot get the value in required format of the property type.

Akanksha Gaur
  • 2,636
  • 3
  • 26
  • 50
  • In Convert.ChangeType(value, property.PropertyType).. If the value is a List, will this works? – Bhuvan Dec 28 '12 at 09:32
  • It will help in assigning the whole list. To add individual elements u need to invoke the "Add" method on the object u got from the property – Akanksha Gaur Dec 28 '12 at 09:37
  • But still i am getting an error as object must implement iConvertible... do i need to implement his for all the methods – Bhuvan Dec 28 '12 at 09:38
  • Also, Did u use ToString() on the item retrieved from the data row? like this lstProductSpec.product_specs_id = Convert.ToInt64(row["product_specs_id"].ToString()); – Akanksha Gaur Dec 28 '12 at 09:41
  • i should not use this because i had defined my product_specs_id as int in my productspec model – Bhuvan Dec 28 '12 at 09:43
  • u r using the wrong type, it should be property.PropertyType property.GetType() will give u typeof(PropertyInfo) – Akanksha Gaur Dec 28 '12 at 10:10
  • Hi.. As i am using List with in a list i am getting my type as "System.Reflection.RuntimePropertyInfo" – Bhuvan Dec 28 '12 at 10:15
  • use property.PropertyType which gives u the type of property – Akanksha Gaur Dec 28 '12 at 10:20
  • PropertyType is retreving my genericList ProductSpec..but when i try i am receiving the same error – Bhuvan Dec 28 '12 at 10:37
  • invoke the "Add" method on the list, that is how u add an object to the list – Akanksha Gaur Dec 28 '12 at 11:02
  • But my list is in a property variable of propertyinfo type.. how do i add my lstproductspec to my propertyinfo type variable – Bhuvan Dec 28 '12 at 11:06