Help with ObjectDataSource binding.

  • Thread starter Thread starter JDeats
  • Start date Start date
J

JDeats

I'm trying to discover where to specify attribute mapping (not sure if
that is the correct term as I'm trying to get my head around this
process) for an ObjectDataSource

My ObjectDataSource in this case is to a class called PersonData which
has Update, Insert, Delete, Select methods. Using the Update method as
an example. The Update method takes in three properties
int id, string fname, string lname, int age

Should I be following a specific naming convention on method params?
If not how does the page compiler know how what to provide to my
Insert method? I don't see a place to specify this or any explanation
on the rules of how this would be inferred by the object.

Below I'm providing a code sample of how my ObjectDataSource is
defined and a editable DataGridView that is bound to this object. On
update I get the following message:

"ObjectDataSource 'ObjectDataSource1' could not find a non-generic
method...". I assume if I could somehow tell the ObjectDataSource
where to pull/map the UpdateMethod parameters this would go away.


<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="PersonData" DeleteMethod="DeletePerson"
UpdateMethod="UpdatePerson"
InsertMethod="InsertPerson" SelectMethod="GetAllPersons">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="fname" Type="String" />
<asp:Parameter Name="lname" Type="String" />
<asp:Parameter Name="age" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="newid" Type="Int32" />
</InsertParameters>
</asp:ObjectDataSource>

<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1"
AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
<asp:BoundField DataField="PersonID"
HeaderText="PersonID"
SortExpression="PersonID" ReadOnly=false />
<asp:BoundField DataField="FName" HeaderText="FName"
SortExpression="FName" ReadOnly=false />
<asp:BoundField DataField="LName" HeaderText="LName"
SortExpression="LName" ReadOnly=false />
<asp:BoundField DataField="Age" HeaderText="Age"
SortExpression="Age" ReadOnly=false />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
 
I'm trying to discover where to specify attribute mapping (not sure if
that is the correct term as I'm trying to get my head around this
process) for an ObjectDataSource

My ObjectDataSource in this case is to a class called PersonData which
has Update, Insert, Delete, Select methods. Using the Update method as
an example. The Update method takes in three properties
  int id, string fname, string lname, int age

Should I be following a specific naming convention on method params?
If not how does the page compiler know how what to provide to my
Insert method? I don't see a place to specify this or any explanation
on the rules of how this would be inferred by the object.

Below I'm providing a code sample of how my ObjectDataSource is
defined and a editable DataGridView that is bound to this object. On
update I get the following message:

"ObjectDataSource 'ObjectDataSource1' could not find a non-generic
method...". I assume if I could somehow tell the ObjectDataSource
where to pull/map the UpdateMethod parameters this would go away.

        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
         TypeName="PersonData" DeleteMethod="DeletePerson"
UpdateMethod="UpdatePerson"
            InsertMethod="InsertPerson" SelectMethod="GetAllPersons">
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32"/>
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="id" Type="Int32"/>
                <asp:Parameter Name="fname" Type="String" />
                <asp:Parameter Name="lname" Type="String" />
                <asp:Parameter Name="age" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="newid" Type="Int32" />
            </InsertParameters>
        </asp:ObjectDataSource>

  <asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1"
        AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333"
        GridLines="None">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
            <asp:BoundField DataField="PersonID"
HeaderText="PersonID"
                SortExpression="PersonID" ReadOnly=false />
            <asp:BoundField DataField="FName" HeaderText="FName"
SortExpression="FName" ReadOnly=false />
            <asp:BoundField DataField="LName" HeaderText="LName"
SortExpression="LName" ReadOnly=false />
            <asp:BoundField DataField="Age" HeaderText="Age"
SortExpression="Age" ReadOnly=false />
        </Columns>
        <PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>


I found my answer and wanted to post it for others who might be
searching this group at a future date. The answer is that the
parameter names are inferred the input method parameter variable names
must match the variables names of the object returned by the
SelectMethod.

So in my case for example, my Update method inside my bound custom
data object looked had this method signature

public UpdatePerson(int id, string fname, string lname, int age)

I had to change that to:
public UpdatePerson(int PersonID, string FName, string LName, int Age)

and for delete i had

public DeletePerson(int id)

which had to be changed to

public DeletePerson(int PersonID)

now everything is resolving as it should... I hope this helps someone
in the future.
 
Back
Top