使用更新和删除时,ObjectDataSource找不到具有参数的非泛型方法

时间:2016-02-01 07:34:02

标签: c# asp.net

您好我收到此错误:

  

" ObjectDataSource' ObjectDataSource1'找不到非通用的   方法' UpdateEmployee'有参数:originalEmployeeId,   originalName,originalGender,originalCity,EmployeeId,Name,Gender,   市"

当我按下更新按钮时。但我的删除按钮没有问题。这是我的代码:
WebForm1.aspx的

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 1);
return df.format(cal.getTime());

Employee2.cs

<form id="form1" runat="server">
<div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="ObjectDataSource1" DataKeyNames="EmployeeId">
        <Columns>
            <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
            <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" 
                SortExpression="EmployeeId" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" 
                SortExpression="Gender" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
        </Columns>
    </asp:GridView>
    <br />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetAllEmployee2S" TypeName="EditingData.Employee2" 
        UpdateMethod="UpdateEmployee" DeleteMethod="DeleteEmployee" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original{0}">
        <DeleteParameters>
            <asp:Parameter Name="originalEmployeeId" Type="Int32" />
            <asp:Parameter Name="originalName" Type="String" />
            <asp:Parameter Name="originalGender" Type="String" />
            <asp:Parameter Name="originalCity" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="originalEmployeeId" Type="Int32" />
            <asp:Parameter Name="originalName" Type="String" />
            <asp:Parameter Name="originalGender" Type="String" />
            <asp:Parameter Name="originalCity" Type="String" />
        </UpdateParameters>
    </asp:ObjectDataSource>

</div>
</form>

Employee2Dal.cs

public static List<Employee2> GetAllEmployee2S()
    {
        var dal = new Employee2Dal();
        return dal.GetAllEmployee2S();
    }
    public static void DeleteEmployee(int originalEmployeeId, string originalName, string originalGender, string originalCity)
    {
        var dal = new Employee2Dal();
        dal.DeleteEmployee(originalEmployeeId, originalName, originalGender, originalCity);
    }
    public static void UpdateEmployee(int originalEmployeeId, string originalName, string originalGender, string originalCity)
    {
        var dal = new Employee2Dal();
        dal.UpdateEmployee(originalEmployeeId, originalName, originalGender, originalCity);
    }

1 个答案:

答案 0 :(得分:1)

您的代码有效,因为Delete方法不需要参数的当前值。因此,您只有旧值参数,其格式为orignial{0}'. But with更新method you have both old and current values. Old values are also formatted with原始{0}`前缀,但当前值与未修改的GridView字段名称一起传递。所以你有8个参数:

originalEmployeeId, originalName, originalGender, originalCity
employeeId, name, gender, city

您可以简单地将缺少的参数添加到UpdateEmployee方法。但我认为你不需要原创价值观。甚至更多 - 除了员工ID之外,您不需要DeleteEmployee方法的任何值。因此,不要使用原始值(注意ConflictDetection应该等于OverwriteChanges以关闭传递给Update方法的原始值):

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    TypeName="EditingData.Employee2"
    SelectMethod="GetAllEmployee2S"  
    UpdateMethod="UpdateEmployee"
    DeleteMethod="DeleteEmployee" ConflictDetection="OverwriteChanges">       
</asp:ObjectDataSource>

让GridView传递与字段名称相对应的参数

public static void DeleteEmployee(int employeeId)
{
    var dal = new Employee2Dal();
    dal.DeleteEmployee(employeeId); // modify dal method
}
public static void UpdateEmployee(int employeeId, string name, string gender, string city)
{
    var dal = new Employee2Dal();
    dal.UpdateEmployee(employeeId, name, gender, city);
}