无法使用添加参数更新asp.net中的表

时间:2018-04-10 15:22:55

标签: c#

我正在尝试使用mys.net中的patientId从asp.net更新表格患者。使用调试器我可以看到更新的执行查询没有被执行。可能是添加参数更新的语法有问题。如果有人能找到解决方案,我将非常感激。

服务类中用于更新患者记录的功能:

 public bool UpdatePatient(AppointmentInfo appointmentInfo)
    {
        bool set = false;
        conn.GetConnection();
        try
        {
            MySqlCommand cmd = conn.GetCommand(Queries.UpdatePatient);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@patientId", Convert.ToInt32(appointmentInfo.patientId));
            cmd.Parameters.AddWithValue("@fName", Convert.ToString(appointmentInfo.fName));
            cmd.Parameters.AddWithValue("@mName", Convert.ToString(appointmentInfo.mName));
            cmd.Parameters.AddWithValue("@lName", Convert.ToString(appointmentInfo.lName));
            cmd.Parameters.AddWithValue("@age", Convert.ToInt32(appointmentInfo.age));
            cmd.Parameters.AddWithValue("@gender", Convert.ToString(appointmentInfo.gender));
            cmd.Parameters.AddWithValue("@address", Convert.ToString(appointmentInfo.address));
            cmd.Parameters.AddWithValue("@emailId", Convert.ToString(appointmentInfo.emailId));
            if (cmd.ExecuteNonQuery()>0) 
            {
                set = true;
            }
            else
            {
                set = false;
            }

        }
        catch (MySqlException)
        {
            set = false;
        }
        conn.CloseConnection();
        return set;
    } 

这里的查询是一个.resx文件,我在这里编写更新查询。查询是

update patient set fName=@fName,mName=@mName,lName=@lName, 
age=@age,gender=@gender,address=@address,emailId=@emailId,
where patientId=@patientId

这是模型类的appointmentInfo

 public class AppointmentInfo
{
    public int appointmentId { get; set;}
    public int patientId { get; set; }
    public string dName { get; set; }

    [Required(ErrorMessage = "Please provide the first name")]
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First Name cannot be longer than 50 characters and less than 1 character")]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use alphabets only please")]
    public string fName { get; set; }

    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use alphabets only please")]
    public string mName { get; set; }

    [Required(ErrorMessage = "Please provide the last name")]
    [StringLength(50, MinimumLength = 1, ErrorMessage = "Last Name cannot be longer than 50 characters and less than 1 character")]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use alphabets only please")]
    public string lName { get; set; }

    [Required]
    [Range(1, 100, ErrorMessage = "Enter an age between 1 and 100")]
    public int age { get; set; }

    [Required(ErrorMessage = "Please provide the gender")]
    public string gender { get; set; }

    [Required(ErrorMessage = "Mobile Number is required")]
    [RegularExpression(@"^(\d{10})$", ErrorMessage = "Please enter proper contact details.")]
    public string phNo { get; set; }

    [Required(ErrorMessage = "Please provide the address")]
    [StringLength(50, MinimumLength = 10, ErrorMessage = "Address cannot be longer than 50 characters and less than 10 characters")]
    public string address { get; set; }

    [Required(ErrorMessage = "The email Id is required")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string emailId { get; set; }

    [Required(ErrorMessage = "Please select an option")]
    public int specialityId { get; set; }
    public string specialityName { get; set; }

    [Required(ErrorMessage = "Please select an option")]
    public int doctorId { get; set; }

    [Required(ErrorMessage = "Please provide the date and time")]
    [DataType(DataType.DateTime)]
    public DateTime dtTime { get; set; }

    [NotMapped]
    public List<SpecialityInfo> specialityCollection { get; set; }

    [NotMapped]
    public List<DoctorInfo> doctorCollection { get; set; }
}  

0 个答案:

没有答案