Search code examples
c#dotnetnukedataprovideridatareader

CBO.FillCollection throwing "No parameterless constructor defined for this object." error


I'm trying to fill a collection from an IDataReader that was returned by another method... for some reason it keeps throwing a "No parameterless constructor defined for this object." error for this line:

List<string> names = CBO.FillCollection<string>(DataProvider.Instance().ExecuteReader("getNames", new SqlParameter("UserId", 1)));

I've tried separating out the parameters so things get initialized separately until I had this:

List<string> names = CBO.FillCollection<string>(nameDataReader);

and I was still getting an error on the same line.

Any ideas?


Solution

  • The clue's in the message. There's no parameterless constructor for System.String, so it can't be created using Activator.CreateInstance, which is what is usually used to dynamically create objects.

    EDIT: A solution would be to use the reader directly:

    var strings = new List<string>();
    using(var reader = DataProvider.Instance().ExecuteReader("getNames", new SqlParameter("UserId", 1)))
    {
        while(reader.Read()) 
            strings.Add(reader[0] as string);
    }