我正在使用GridView来显示和编辑数据,但是当我点击GridView左侧的更新时,我收到一条错误消息:
ObjectDataSource'ObjectDataSource1'找不到具有参数的非泛型方法'updateEmployee':old_EmployeeID,old_EmployeeName,old_DepartmentID, EmployeeName ,DepartmentID,EmployeeID。
请参阅下面的网页代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" DataSourceID="ObjectDataSource1" DataKeyNames="EmployeeID">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName"
SortExpression="EmployeeName" />
<asp:TemplateField HeaderText="DepartmentID" SortExpression="DepartmentID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("DepartmentID") %>'>
<asp:ListItem>Select Department</asp:ListItem>
<asp:ListItem Value="1">SM</asp:ListItem>
<asp:ListItem Value="2">CDS</asp:ListItem>
<asp:ListItem Value="3">AM</asp:ListItem>
<asp:ListItem Value="4">FS</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DepartmentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="getAllEmployees"
TypeName="WebApplication1.Learning.Updating.TblEmployeeDataAccessLayer"
UpdateMethod="updateEmployee" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="old_{0}">
<UpdateParameters>
<asp:Parameter Name="old_EmployeeID" Type="Int32" />
<asp:Parameter Name="old_EmployeeName" Type="String" />
<asp:Parameter Name="old_DepartmentID" Type="Int32" />
<asp:Parameter Name="EmployeeName" Type="String" />
<asp:Parameter Name="DepartmentID" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
然后请参阅我的数据访问类和方法:
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int DepartmentID { get; set; }
}
public class TblEmployeeDataAccessLayer
{
public static List<Employee> getAllEmployees()
{
List<Employee> employees = new List<Employee>();
String sqlStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (OracleConnection ocon = new OracleConnection(sqlStr))
{
OracleCommand ocmd = new OracleCommand("select * from tblemployee", ocon);
ocmd.Connection.Open();
OracleDataReader rdr = ocmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.EmployeeID = Convert.ToInt32(rdr["EMPLOYEEID"]);
employee.EmployeeName = rdr["NAME"].ToString();
employee.DepartmentID = Convert.ToInt32(rdr["DEPTID"]);
employees.Add(employee);
}
}
return employees;
}
public static void updateEmployee(int old_EmployeeID, string old_EmployeeName, int old_DepartmentID,
string EmployeeName, int DepartmentID)
{
string sqlCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
StringBuilder sb = new StringBuilder();
sb.Append("update tblemployee set name = :name, deptid = :deptid where employeeid = :empid ");
sb.Append(" and name = :old_name and deptid = :old_deptid");
using (OracleConnection ocon = new OracleConnection(sqlCon))
{
OracleCommand ocmd = new OracleCommand(sb.ToString(), ocon);
ocmd.Parameters.Add(":name", EmployeeName);
ocmd.Parameters.Add(":deptid", DepartmentID);
ocmd.Parameters.Add(":empid", old_EmployeeID);
ocmd.Parameters.Add(":old_name", old_EmployeeName);
ocmd.Parameters.Add(":old_deptid", old_DepartmentID);
ocmd.Connection.Open();
ocmd.ExecuteNonQuery();
}
}
}
所以,显然在updateEmployee方法中只有5个参数,但我感到困惑的是,错误消息说找不到具有参数的非泛型方法'updateEmployee':
old_EmployeeID,old_EmployeeName,old_DepartmentID, EmployeeName ,DepartmentID,EmployeeID
似乎它试图寻找有6个参数的方法,为什么?任何人都可以告诉我原因吗?