TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

Reflection and the ObjectDataSource Control for ASP.NET 2.0

Interesting bit of information here regarding the ObjectDataSource control in ASP.NET 2.0. Let’s say I have an class that I want to set as the data source and the class contains a method that returns a collection. For example, a Person class that has a method named GetPeople as follows:

public List<Person> GetPeople() {

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

    list.Add(new Person("Sean", 32));

    list.Add(new Person("Brittany", 2));

    return list;

}

Behind the scenes, the ObjectDataSource control uses reflection to invoke the method GetPeople() behind the scenes based on the server side tags as follows:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="Person" SelectMethod="GetPeople"></asp:ObjectDataSource>

On the same page, I have grid view that is bound to the ObjectDataSource control as follows.

<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" ForeColor="#333333"

    GridLines="None" CellPadding="4">

    <FooterStyle ForeColor="White" Font-Bold="True" BackColor="#5D7B9D" />

    <RowStyle ForeColor="#333333" BackColor="#F7F6F3" />

    <PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" />

    <SelectedRowStyle ForeColor="#333333" Font-Bold="True" BackColor="#E2DED6" />

    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#5D7B9D" />

    <EditRowStyle Font-Italic="False" Font-Bold="False" BackColor="#999999" />

    <AlternatingRowStyle ForeColor="#284775" BackColor="White" />

</asp:GridView>

The first time I tried running my little test app, I kept getting a run-time exception that said, “The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns.  Ensure that your data source has content.” Yuck!

It turns out that the reflection happening behind the scenes does not work with fields.

public class Person {

    ...

 

    //Uh oh! Won't work!!!!!!!

    public string Name;

    public int Age;

    ...

   

Well, it's not that I buy into what JayBaz says about properties. I was just hastily writing some test code without any regard to my flippancy. As soon as a changed the public fields to properties, TADA!

public class Person {

    private string _name;

    private int _age;

 

    public Person() { }

 

    public Person(string name, int age) {

        this._name = name;

        this._age = age;

    }

 

    public string Name {

        get { return _name; }

        set { _name = value; }

    }

 

    public int Age {

        get { return _age; }

        set { _age = value; }

    }

 

    public List<Person> GetPeople() {

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

        list.Add(new Person("Sean", 32));

        list.Add(new Person("Brittany", 2));

        return list;

    }

}

 

Ever hear the phrase “read the f---ing manual?” Well, it does turn out that the docs tell you this if you dig in far enough. Since ASP.NET 2.0 is still in BETA, I figured I’d share this in case anyone else runs into the same issue.  J


Digg!

posted on Saturday, January 22, 2005 12:56 PM

Feedback

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 7/7/2005 6:26 AM Joe

I had the problem and your solution resolved it.

Thanks!

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 8/16/2005 9:52 PM jamiec

Well, this requires us to hand code the Properties to the class. I am looking for a way which can let us dynamically add Properties to the code, because there are times you may not know what properties(field) you have up front. For example, you may would like to use ObjectDataSource to read from an excel which you don't know the column name up front.

Does anyone know how to do this in ASP.NET 2?

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 8/17/2005 9:17 AM Sean Chase

Why not create a DataSet out of the Excel data and then use regular ol' data binding?

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 8/23/2005 3:42 PM jamiec

Sean,

Thanks for the hint, can you elaborate this more of using DataSet out of excel data? I am actually looking into ways to pump the data from excel to a dataset?

Thanks

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 8/23/2005 8:13 PM Sean Chase

Sure, basically you are just going to use the System.Data.OleDb namespace. There's lots of examples on google. Check out..

http://www.google.com/search?hl=en&q=Excel+to+dataset

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 8/25/2005 5:21 AM jamiec

Got it. Many thanks, Sean.

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 1/19/2007 1:39 PM Monty

Dang, mine are public properties and I'm still getting this. Grrr.

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 2/5/2007 2:31 PM Interesting I looked all over for this answer.

I’ve been programming for many years but the .net 2.0 is very different. As a test to get up to speed I downloaded the personal starter kit. I used the same property setting and style with the ObjectDataSource exactly the same as in the example it would not return data and the fields were not in order as what you expect with the sql server object.
Microsoft’s example doesn’t use SET to set any values but it works in the kit. Is there something else going on behind the scenes with the ObjectDataSource that someone may know?

# re: Reflection and the ObjectDataSource Control for ASP.NET 2.0 2/5/2007 4:32 PM Nevermind

Hey, forget what I said in the previous post. The solution you posted here works. I would have the correct results if I copied and pasted my list of variables correctly. So, killed two birds with one stone. Your solution was a great help... No I just need to pay closer attention to variables