2

下面是我的更新查询,它工作正常,但是当我int DrId(最后)添加参数字段以检查 where 条件时,在此之后它向我显示以下错误

No value given for one or more required parameters. 错误在哪里,因为DrId是我的主键,我不会更新它

public bool UpdateDrMaster(string title, string FirstName, string Mname, string lastName, string regNo, string speciality, string contactNo1, string contactNo2, string city, string addrss, string pincode, string emailId, string fee_unit, string fee, string DrType, bool displayType,int DrId)
        {
            bool flag = false;
            try
            {
                string sql = "UPDATE Doctor_Master SET [Title]=@Title,[FirstName]=@FirstName,[MiddleName]=@MiddleName,[LastName]=@LastName,[RegistrationNo]=@RegistrationNo,[Speciality]=@Speciality,[ContactNo_1]=@ContactNo_1,[ContactNo_2]=@ContactNo_2,[City]=@City,[Address]=@Address,[Pincode]=@Pincode,[Email_ID]=@Email_ID,[Fee_Unit]=@Fee_Unit,[Fee]=@Fee,[Type_of_Dr]=@Type_of_Dr,[Display]=@Display,[Hide_Date]=@Hide_Date WHERE Doctor_ID=" +DrId;

                cmd = new OleDbCommand(sql, acccon);
                 cmd.Parameters.AddWithValue("@Title", title);
                 cmd.Parameters.AddWithValue("@FirstName", FirstName);
                 cmd.Parameters.AddWithValue("@MiddleName", Mname);
                 cmd.Parameters.AddWithValue("@LastName", lastName);
                 cmd.Parameters.AddWithValue("@RegistrationNo", regNo);
                 cmd.Parameters.AddWithValue("@Speciality", speciality);
                 cmd.Parameters.AddWithValue("@ContactNo_1", contactNo1);
                 cmd.Parameters.AddWithValue("@ContactNo_2", contactNo2);
                 cmd.Parameters.AddWithValue("@City", city);
                 cmd.Parameters.AddWithValue("@Address", addrss);
                 cmd.Parameters.AddWithValue("@Pincode", pincode);
                 cmd.Parameters.AddWithValue("@Email_ID", emailId);
                 cmd.Parameters.AddWithValue("@Fee_Unit", fee_unit);
                 cmd.Parameters.AddWithValue("@Fee", fee);
                 cmd.Parameters.AddWithValue("@Type_of_Dr", DrType);
                 cmd.Parameters.AddWithValue("@Display", displayType);
                 cmd.Parameters.AddWithValue("@Hide_Date", System.DateTime.Now.ToShortDateString());                            
                cmd.ExecuteNonQuery();
                flag = true;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message.ToString());
            }
            return flag;
        }
4

2 回答 2

1

为什么不试试这个!!!将您的参数名称替换为 ?

 public bool UpdateDrMaster(string title, string FirstName, string Mname, 
    string lastName, string regNo, string speciality, string contactNo1, 
    string contactNo2, string city, string addrss, string pincode, string emailId, 
    string fee_unit, string fee, string DrType, bool displayType,int DrId)
            {
                bool flag = false;
                try
                {
                    string sql = "UPDATE Doctor_Master SET Title=?FirstName=?,
    MiddleName=?,LastName=?,RegistrationNo=?,Speciality=?,ContactNo_1=?,
    ContactNo_2=?,City=?,Address=?,Pincode=?,Email_ID=?,Fee_Unit=?,Fee=?,
    Type_of_Dr=?,Display=?,Hide_Date=? WHERE Doctor_ID=?";

                    cmd = new OleDbCommand(sql, acccon);
                     cmd.Parameters.AddWithValue("@Title", title);
                     cmd.Parameters.AddWithValue("@FirstName", FirstName);
                     cmd.Parameters.AddWithValue("@MiddleName", Mname);
                     cmd.Parameters.AddWithValue("@LastName", lastName);
                     cmd.Parameters.AddWithValue("@RegistrationNo", regNo);
                     cmd.Parameters.AddWithValue("@Speciality", speciality);
                     cmd.Parameters.AddWithValue("@ContactNo_1", contactNo1);
                     cmd.Parameters.AddWithValue("@ContactNo_2", contactNo2);
                     cmd.Parameters.AddWithValue("@City", city);
                     cmd.Parameters.AddWithValue("@Address", addrss);
                     cmd.Parameters.AddWithValue("@Pincode", pincode);
                     cmd.Parameters.AddWithValue("@Email_ID", emailId);
                     cmd.Parameters.AddWithValue("@Fee_Unit", fee_unit);
                     cmd.Parameters.AddWithValue("@Fee", fee);
                     cmd.Parameters.AddWithValue("@Type_of_Dr", DrType);
                     cmd.Parameters.AddWithValue("@Display", displayType);
                     cmd.Parameters.AddWithValue("@Hide_Date", System.DateTime.Now.ToShortDateString()); 
      cmd.Parameters.AddWithValue("@DrId", DrId);                           
                    cmd.ExecuteNonQuery();
                    flag = true;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message.ToString());
                }
                return flag;
            }

另一个可能的原因

将您的 Db 列名称与查询列匹配。查看是否有任何字符放错位置或拼写错误

于 2013-10-05T11:13:21.047 回答
1

Doctor_Id在更新查询中有,但您没有为它设置值。即使您不打算更新它的值,您也必须为其设置一个值作为您的 where 类的一部分。

string sql = "UPDATE Doctor_Master SET [Title]=@Title,[FirstName]=@FirstName,[MiddleName]=@MiddleName,[LastName]=@LastName,[RegistrationNo]=@RegistrationNo,[Speciality]=@Speciality,[ContactNo_1]=@ContactNo_1,[ContactNo_2]=@ContactNo_2,[City]=@City,[Address]=@Address,[Pincode]=@Pincode,[Email_ID]=@Email_ID,[Fee_Unit]=@Fee_Unit,[Fee]=@Fee,[Type_of_Dr]=@Type_of_Dr,[Display]=@Display,[Hide_Date]=@Hide_Date WHERE Doctor_ID=" +DrId;
于 2013-10-05T11:05:24.880 回答