0

I am trying to write a function that can take three parameters: an SqlDataReader, a DataGridView and a List.

I want to take the contents of the SqlDataReader and create a list of Objects, then bind this to the DataGridView.

With a few pointers from another Stack Overflow user I came up with the following:

public void FillArrayList<T>(DataGridView grid, SqlDataReader reader, List<T> list)
{
    //Fill the list with the contents of the reader
    while (reader.Read())
    {
        Object obj = new Object();
        Type type = typeof(T);

        FieldInfo[] fields = type.GetFields(); // Get the fields of the assembly
        int i = 0;

        foreach(var field in fields)
        {
            field.SetValue(obj, reader[i]); // set the fields of T to the reader's value
            i++;
        }

        list.Add((T)obj);
    }

    grid.DataSource = list;
}

When I run the code, I get an error when casting the object to type T:

Unable to cast object of type 'System.Object' to type 'TestHarness.Organisation'.

I was under the impression that an Object could store anything. Can anyone advise me on why this cast cannot be performed?

Thanks,

Andy

1

2 Answers 2

2

You can cast nearly anything to Object, but you can't cast Object to anything. Take a look at the System.Object class on MSDN. Notice how there's almost nothing there. The cast doesn't make sense because it's functionally the same as doing calling a new TestHarness.Organization.

If you know exactly what you're looking for in the DataReader to go into TestHarness.Organization or whatever it is, you might try a user-defined conversion. This would allow you to either implicitly or explicitly call some code to do the type change for you without any extra code on the spot.

2
  • Hi, thank you for your reply. I did originally have a method that took an OrganisationList and an OrganisationGrid. Easy to implement, but I needed a very similar method to fill a JobGrid with the contents of a JobList. I saw that the code could be improved by writing a method that could handle a list of anything. Is it possible to add an unknown Object to a generic List<T>?
    – amarsha4
    Jun 11, 2012 at 19:42
  • Not as such, no. You can generically declare a List<T> but for compilation purposes everything will have to be a T as you add it in, which isn't very useful because you can't access anything about T. I think what you're actually looking for is a good implementation of interfaces. If you were to make OrgGrid and JobGrid both implement the interface IWorkGrid or whatever you call it, you can then make a List<IWorkGrid> and do what you want with that.
    – tmesser
    Jun 11, 2012 at 19:56
0

With a little help from the link posted by MMK, in the comment below my question, I have devised a solution:

public void FillList<T>(DataGridView grid, string SQLCommand, List<T> list) where T : class, new()
    {
        //Load the data into the SqlDataReader
        SqlCommand dataCommand = new SqlCommand();
        dataCommand.Connection = dataConnection;
        dataCommand.CommandType = CommandType.Text;
        dataCommand.CommandText = SQLCommand;

        SqlDataReader dataReader = dataCommand.ExecuteReader();

        //Fill the list with the contents of the reader
        while (dataReader.Read())
        {
            var obj = new T();

            //Get the property information
            PropertyInfo[] properties = typeof(T).GetProperties();
            int i = 0;

            foreach(var property in properties)
            {
                property.SetValue((T)obj, dataReader[i], null); // set the fields of T to the reader's value
                i++;
            }

            list.Add(obj);
        }

        dataReader.Close();

        //Bind the list to the DataGridView
        grid.DataSource = list;
    }

Seems to do exactly what I need. It's possible I may come across some obvious error as I use this in more and more situations, but, this is life.

Thanks for your help, guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.