tags:

views:

66

answers:

3

While executing this method:

public static List<T> ToList<T>(DataTable dataTable)
        {
            Type type = typeof(T);

            List<T> list = new List<T>();

            foreach (DataRow dr in dataTable.Rows)
            {
                object[] args = new object[1];

                args[0] = dr;

                list.Add((T)Activator.CreateInstance(type, args));
            }

            return list;
        }

I am getting this exception:

Constructor on type 'Northwind.BO.Products' not found.

But I know that I already have declared a constructor in my Products class.

public class Products
{
 private int _ProductID;
 [DataMapping("ProductID", -99)]
 public int ProductID
 {
  get { return _ProductID; }
  set
  {
   if (_ProductID <= 0)
   {
    _ProductID = value;
   }
   else
   {
    throw new Exception("ID should not be set manually!");
   }
  }
 }

 private string _ProductName;
 [DataMapping("ProductName", "")]
 public string ProductName
 {
  get { return _ProductName; }
  set { _ProductName = value;}
 }

 private int _SupplierID;
 [DataMapping("SupplierID", -99)]
 public int SupplierID
 {
  get { return _SupplierID; }
  set { _SupplierID = value;}
 }

 private int _CategoryID;
 [DataMapping("CategoryID", -99)]
 public int CategoryID
 {
  get { return _CategoryID; }
  set { _CategoryID = value;}
 }

 private string _QuantityPerUnit;
 [DataMapping("QuantityPerUnit", "")]
 public string QuantityPerUnit
 {
  get { return _QuantityPerUnit; }
  set { _QuantityPerUnit = value;}
 }

 private decimal _UnitPrice;
 [DataMapping("UnitPrice", -99.99)]
 public decimal UnitPrice
 {
  get { return _UnitPrice; }
  set { _UnitPrice = value;}
 }

 private short _UnitsInStock;
 [DataMapping("UnitsInStock", -99)]
 public short UnitsInStock
 {
  get { return _UnitsInStock; }
  set { _UnitsInStock = value;}
 }

 private short _UnitsOnOrder;
 [DataMapping("UnitsOnOrder", -99)]
 public short UnitsOnOrder
 {
  get { return _UnitsOnOrder; }
  set { _UnitsOnOrder = value;}
 }

 private short _ReorderLevel;
 [DataMapping("ReorderLevel", -99)]
 public short ReorderLevel
 {
  get { return _ReorderLevel; }
  set { _ReorderLevel = value;}
 }

 private bool _Discontinued;
 [DataMapping("Discontinued", false)]
 public bool Discontinued
 {
  get { return _Discontinued; }
  set { _Discontinued = value;}
 }

public Products(object[] args)
{
}

public Products()
{
}
+1  A: 

You don't need a constructor on any Northwind class - according to the exception, you need the correct constructor on Northwind.BO.Products.

Furthermore, as used by Activator.CreateInstance, this constructor must have the right signature that matches the argument you pass to it. Since you are passing an argument to it, the default constructor will not match.

As I read your question, it must have this constructor

public Products(DataRow dr)
Mark Seemann
The object array is part of the CreateInstance call; CreateInstance will try to find a constructor that matches the contents so the array best. http://msdn.microsoft.com/en-us/library/wcxyzt4d.aspx
dtb
This is not working either. public Products(object[] args){}
JMSA
@dtb: You are right - my fault. Edited my answer.
Mark Seemann
+2  A: 

The problem is that the Northwind.BO.Products doesn't have a public contructor that takes a single DataRow.

Can you tell us what constructors it has?

SLaks
public Products(){}
JMSA
Then you can only write `Activator.CreateInstance(type)` without any parameters.
SLaks
A: 

The constructor most signature probably does not match the argument you're supplying.

Dave Markle