Pregunta

I have aproblem everytime I call my update function, I belive that the source of the problem is because this function gets object paramter, and also get a paramter from diffrant souce, and i am not doing it currectly.

This is the error: http://prntscr.com/2z0cd6

GridView code:

<asp:GridView ID="gvAnimals" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ObjectDataSourceAnimals" ForeColor="#333333" GridLines="None" DataKeyNames="animalId">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    <asp:BoundField DataField="animalId" HeaderText="animalId" SortExpression="animalId" />
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                    <asp:CheckBoxField DataField="vertebrates" HeaderText="vertebrates" SortExpression="vertebrates" />
                    <asp:CheckBoxField DataField="vegetarian" HeaderText="vegetarian" SortExpression="vegetarian" />
                    <asp:CheckBoxField DataField="terrestrial" HeaderText="terrestrial" SortExpression="terrestrial" />
                    <asp:BoundField DataField="kind" HeaderText="kind" SortExpression="kind" />
                    <asp:BoundField DataField="avgWeight" HeaderText="avgWeight" SortExpression="avgWeight" />
                    <asp:BoundField DataField="avgHeight" HeaderText="avgHeight" SortExpression="avgHeight" />
                    <asp:BoundField DataField="infoAdress" HeaderText="infoAdress" SortExpression="infoAdress" />
                    <asp:BoundField DataField="imageAdress" HeaderText="imageAdress" SortExpression="imageAdress" />
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
            <asp:ObjectDataSource ID="ObjectDataSourceAnimals" runat="server" DeleteMethod="DeleteAnimal" OldValuesParameterFormatString="original_{0}" SelectMethod="GetAllAnimals" TypeName="BLProject.Animal" UpdateMethod="UpdateAnimal" DataObjectTypeName="BLProject.Animal">
                <DeleteParameters>
                    <asp:Parameter Name="a" Type="Object" />
                    <asp:SessionParameter Name="newsAdress" SessionField="Adress" Type="String" />
                </DeleteParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="textSearch" Name="Search" PropertyName="Text" Type="String" />
                    <asp:ControlParameter ControlID="dropdownlistAnimalToSearch" Name="type" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="a" Type="Object" />
                    <asp:SessionParameter DefaultValue="D:\\project\\Ilan Project 29-11\\Web\\NewsInfo" Name="newsAdress" SessionField="Adress" Type="String" />
                </UpdateParameters>
            </asp:ObjectDataSource>

*I belive that the source of the error is in the bold lines

The Update function:

[DataObjectMethod(DataObjectMethodType.Update)]
    public static int UpdateAnimal(Animal a, string newsAdress)

Animal a is the object paramter which the grid view get by using the select function, but the adress is another paramter from session source which the function gets too. However, since I added this paramter I have this new error.

Thanks.

¿Fue útil?

Solución

You can do it like this:

Change your method with this one

public void UpdateAnimal(int animalId, string name, bool vertebrates, bool vegetarian, string newsAdress)
    {

    }

and your ObjectDataSource

<asp:ObjectDataSource ID="ObjectDataSourceAnimals" runat="server" DeleteMethod="DeleteAnimal" SelectMethod="GetAllAnimals" TypeName="BLProject.Animal" UpdateMethod="UpdateAnimal">
            <DeleteParameters>
                <asp:Parameter Name="a" Type="Object" />
                <asp:SessionParameter Name="newsAdress" SessionField="Adress" Type="String" />
            </DeleteParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="textSearch" Name="Search" PropertyName="Text" Type="String" />
                <asp:ControlParameter ControlID="dropdownlistAnimalToSearch" Name="type" PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="animalId" Type="Int32"/>
                .......the other fields from Animal that you want to update
                <asp:Parameter Name="name" Type="String"/>
                <asp:Parameter Name="vegetarian" Type="Boolean"/>
                <asp:Parameter Name="vertebrates" Type="Boolean"/>
                <asp:SessionParameter DefaultValue="D:\\project\\Ilan Project 29-11\\Web\\NewsInfo" Name="newsAdress" SessionField="Adress" Type="String" />
            </UpdateParameters>
        </asp:ObjectDataSource>

Seems that when using DataObjectTypeName you cannot add parameters. The solution is to send some or all the properties of the Animal row edited as parameters plus the other parameter.

If you want to use OldValuesParameterFormatString="original_{0}", add this int original_animalid to the method input parameters.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top