2014-03-12 74 views
1

我使用此代码更新文本框中的数据...此代码位于更新按钮中,一旦我做出更改并单击该按钮,错误消息出现更新过程中命令执行期间遇到致命错误

 try 
     { 
      MySqlConnection connection = new MySqlConnection(MyConnectionString); 
      MySqlCommand cmd; 
      connection.Open(); 
      cmd = connection.CreateCommand(); 
      cmd.CommandText = "UPDATE student_info SET SEM = @SEM, STUDENT_NO = @STUDENT_NO, LASTNAME = @LASTNAME" + 
      ", FIRSTNAME = @FIRSTNAME, MIDDLENAME = @MIDDLENAME, CITY = @CITY, STREET = @STREET, GENDER = @GENDER" + 
      ", COURSE = @COURSE, YEAR = @YEAR, SECTION = @SECTION, BIRTHDAY = @BIRTHDAY Where STUDENT_NO = @STUDENT_NO"; 

      cmd.Parameters.AddWithValue("@SEM", sem_combo.Text); 
      cmd.Parameters.AddWithValue("@STUDENT_NO", studentNo_txt.Text); 
      cmd.Parameters.AddWithValue("@LASTNAME", lname_txt.Text); 
      cmd.Parameters.AddWithValue("@FIRSTNAME", fname_txt.Text); 
      cmd.Parameters.AddWithValue("@MIDDLENAME", mname_txt.Text); 
      cmd.Parameters.AddWithValue("@CITY", address_txt.Text); 
      cmd.Parameters.AddWithValue("@STREET", street_txt.Text); 
      cmd.Parameters.AddWithValue("@GENDER", gender_combo.Text); 
      cmd.Parameters.AddWithValue("@COURSE", program_combo.Text); 
      cmd.Parameters.AddWithValue("@YEAR", yr_combo.Text); 
      cmd.Parameters.AddWithValue("@SECTION", section_combo.Text); 
      cmd.Parameters.AddWithValue("@BIRTHDAY", bday.Text); 
      cmd.ExecuteNonQuery(); 
      cmd.Parameters.Clear(); 

      cmd.CommandText = "UPDATE contacts SET EMAIL = @EMAIL, CELL_NO = @CELL_NO Where STUDENT_NO = @STUDENT_NO"; 

      cmd.Parameters.AddWithValue("@EMAIL", email_txt.Text); 
      cmd.Parameters.AddWithValue("@CELL_NO", contact_txt.Text); 
      cmd.ExecuteNonQuery(); 
      cmd.Parameters.Clear(); 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
+0

错误消息是命令执行期间遇到的致命错误 –

回答

2

入住此行

  cmd.Parameters.Clear(); 

      cmd.CommandText = "UPDATE contacts SET EMAIL = @EMAIL, 
      CELL_NO = @CELL_NO Where STUDENT_NO = @STUDENT_NO"; 

      cmd.Parameters.AddWithValue("@EMAIL", email_txt.Text); 
      cmd.Parameters.AddWithValue("@CELL_NO", contact_txt.Text); 
      cmd.ExecuteNonQuery(); 
      cmd.Parameters.Clear(); 

更改为

  cmd.Parameters.Clear(); 
      cmd.CommandText = "UPDATE contacts SET EMAIL = @EMAIL, 
      CELL_NO = @CELL_NO Where STUDENT_NO = @STUDENT_NO"; 

      cmd.Parameters.AddWithValue("@EMAIL", email_txt.Text); 
      cmd.Parameters.AddWithValue("@CELL_NO", contact_txt.Text); 
      cmd.Parameters.AddWithValue("@STUDENT_NOL",studentNo_txt.Text); 
      cmd.ExecuteNonQuery(); 
      cmd.Parameters.Clear(); 

您已清除参数,但之后使用@STUDENT_NO参数。此参数在声明后没有声明任何参数

+0

谢谢,帮我找到了我的错误!这应该是显而易见的,但是当你惊慌失措时,很容易忽视小事。 – SeanKendle

相关问题