C# – Proper ObjectDataSource Use

asp.netasp.net-2.0c#-3.0datagridviewcheckboxcellobjectdatasource

Greetings!

I'm creating a User Control that will display data in a GridView control. We are using n-tier architecture and the data in question is retrieved from our database and returned to us as a ReadOnlyCollection. OurNewObject is a class containing several properties and an empty constructor that takes no parameters – it's in the following namespace: Acme.ObjectModel.

In the user control, I have the following:

<asp:GridView ID="ourGrid" runat="server" DataSourceID="ourDataSource">
    <columns>
    <asp:BoundField DataField="Name" HeaderText="Full Name" />
    <asp:BoundField DataField="Gender" HeaderText="Gender" />
    <asp:BoundField DataField="BirthYear" HeaderText="Year of Birth" />
    <asp:BoundField DataField="JoinDate" HeaderText="Date Joined" />
  </columns>
</asp:GridView>
<asp:ObjectDataSource ID="ourDataSource" runat="server" SelectMethod="GetTopUsers" TypeName="Acme.Model.OurNewObject">
</asp:ObjectDataSource>

In the User Control's code behind, I have the following public method:

public ReadOnlyCollection<OurNewObject> GetTopUsers()
{
    return (OurDataProxy.GetJustTheTopUsers());
}

When I place the User Control on a Web form and run it, I get the following message:

ObjectDataSource 'ourDataSource' could not find a non-generic method 'GetTopUsers' that has no parameters.

So my questions are:

  1. Am I using the ObjectDataSource
    incorrectly?
  2. Is there a more proper way to use the ObjectDataSource in this situation?

Thanks.

Best Answer

I believe the issue is missing two attributes.

First on your GetTopUsers() Method add this attribute

[System.ComponentModel.DataObjectMethodAttribute
    (System.ComponentModel.DataObjectMethodType.Select, true)]

Then on the actual OurNewObject class add this attribute

[System.ComponentModel.DataObject]
Related Topic