尝试从gridview更新数据时出现此错误:
System.InvalidOperationException:ObjectDataSource'ObjectDataSource1' 找不到具有参数的非泛型方法'updateEmp': id,name,u_name,pass,mail,address,city,dob,cnic,designation, ph_no,emp_name,emp_id。
以下是我的对象数据源的Asp代码
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="deleteEmp" SelectMethod="GetAllEmployees" TypeName="fmsWeb.datagridDemo" UpdateMethod="updateEmp" ConflictDetection="OverwriteChanges">
<DeleteParameters>
<asp:Parameter Name="emp_id" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="String" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="u_name" Type="String" />
<asp:Parameter Name="pass" Type="String" />
<asp:Parameter Name="mail" Type="String" />
<asp:Parameter Name="address" Type="String" />
<asp:Parameter Name="city" Type="String" />
<asp:Parameter Name="dob" Type="String" />
<asp:Parameter Name="cnic" Type="String" />
<asp:Parameter Name="designation" Type="String" />
<asp:Parameter Name="ph_no" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
这是我定义方法的类代码
public static List<Employees> GetAllEmployees()
{
List<Employees> listemp = new List<Employees>();
using (MySqlConnection con = new MySqlConnection(@"server=127.0.0.1;user id=root;database=fms"))
{
MySqlCommand cmd = new MySqlCommand("Select * from emp", con);
con.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employees em = new Employees();
em.emp_id = dr[0].ToString();
em.emp_name = dr[1].ToString();
em.u_name = dr[2].ToString();
em.pass = dr[3].ToString();
em.mail = dr[4].ToString();
em.address = dr[5].ToString();
em.city = dr[6].ToString();
em.dob = dr[7].ToString();
em.cnic = dr[8].ToString();
em.designation = dr[9].ToString();
em.ph_no = dr[10].ToString();
listemp.Add(em);
}
}
return listemp;
}
public static void deleteEmp(String emp_id)
{
using (MySqlConnection con = new MySqlConnection(@"server=127.0.0.1;user id=root;database=fms"))
{
MySqlCommand cmd = new MySqlCommand("Delete from emp where emp_id=@empID", con);
MySqlParameter param = new MySqlParameter("empID", emp_id);
cmd.Parameters.Add(param);
con.Open();
cmd.ExecuteNonQuery();
}
}
public static string updateEmp(String id, String name, String u_name, String pass, String mail, String address, String city, String dob, String cnic, String designation,String ph_no)
{
using (MySqlConnection con = new MySqlConnection(@"server=127.0.0.1;user id=root;database=fms"))
{
MySqlCommand cmd = new MySqlCommand("UPDATE `emp` SET `emp_id`= @id,`emp_name`=@name,`u_name`=@u_name,`pass`=pass,`mail`=@mail,`address`=@address,`city`=@city,`dob`=@dob,`cnic`=@cnic,`designation`=@designation,`ph_no`=@ph_no WHERE emp_id=@id",con);
MySqlParameter paramID = new MySqlParameter("@id", id);
cmd.Parameters.Add(paramID);
MySqlParameter pname = new MySqlParameter("@name", name);
cmd.Parameters.Add(pname);
MySqlParameter pu_name = new MySqlParameter("@u_name", u_name);
cmd.Parameters.Add(pu_name);
MySqlParameter p_pass = new MySqlParameter("@pass", pass);
cmd.Parameters.Add(p_pass);
MySqlParameter pmail = new MySqlParameter("@mail", mail);
cmd.Parameters.Add(pmail);
MySqlParameter add = new MySqlParameter("@address", address);
cmd.Parameters.Add(add);
MySqlParameter pcity = new MySqlParameter("@city", city);
cmd.Parameters.Add(pcity);
MySqlParameter pdob = new MySqlParameter("@dob", dob);
cmd.Parameters.Add(pdob);
MySqlParameter pcnic = new MySqlParameter("@cnic", cnic);
cmd.Parameters.Add(pcnic);
MySqlParameter desig = new MySqlParameter("@designation", designation);
cmd.Parameters.Add(desig);
MySqlParameter ph = new MySqlParameter("@ph_no", ph_no);
cmd.Parameters.Add(ph);
con.Open();
int i=cmd.ExecuteNonQuery();
return Convert.ToString(i);
}
}
选择和删除方法工作正常,但在更新时我有错误
答案 0 :(得分:1)
您的Emloyee
objcet没有id
和name
属性。
它有emp_id
和emp_name
。
更改参数以反映:
而不是:
<asp:Parameter Name="id" Type="String" />
<asp:Parameter Name="name" Type="String" />
这样做:
<asp:Parameter Name="emp_id" Type="String" />
<asp:Parameter Name="emp_name" Type="String" />
答案 1 :(得分:0)
将您的查询更改为:
"UPDATE emp SET emp_name=@name,u_name=@u_name,pass=@pass,mail=@mail,address=@address,city=@city,dob=@dob,cnic=@cnic,designation=@designation,ph_no=@ph_no WHERE emp_id=@id"
AS您的搜索约束是id,最好避免更新它,以防它是某种主要或唯一的密钥