2016-10-15 63 views
1

下面是我添加按鈕的代碼,每次我點擊一次添加按鈕給這個錯誤拋出異常:「System.Data.SqlClient.SqlException」在System.Data.dll中

command.ExecuteNonQuery();可以幫我這個問題?

這裏是我的代碼

private void button4_Click(object sender, EventArgs e) //Add button 
     { 
      tbLecturerId.Select(); 

      string strID, strFirstName, strUsername, strPassword, strDepartment, strEmail; 

      strID = tbLecturerId.Text; 
      strFirstName = tbLname.Text; 
      strUsername = Usernametxt.Text; 
      strPassword = Passwordtxt.Text; 
      strDepartment = cbDepartment.Text; 
      strEmail = tbEmail.Text; 


      connect.Open(); 
      SqlCommand command = new SqlCommand(@"INSERT INTO Lecturer_tbl (LecturerID,LecturertName, Username,Password, Department,Email) VALUES('" + strID + "','" + strFirstName + "','" + strUsername + "', '" + strPassword + "','" + strDepartment + "' ,'" + strEmail + "')", connect); 
      command.ExecuteNonQuery(); 

      connect.Close(); 

      displayLectureGrid(); 
      clearLecturertbl(); 
+2

粘貼異常堆棧跟蹤沒有辦法猜測細節 –

回答

1

。在你的語句中的空格和逗號的問題。試試這個

SqlCommand command = new SqlCommand(@"INSERT INTO Lecturer_tbl (LecturerID,LecturertName,Username,Password,Department,Email) VALUES('" + strID + "','" + strFirstName + "','" + strUsername + "','" + strPassword + "','" + strDepartment + "','" + strEmail + "')", connect); 
+0

謝謝。代碼完美工作。 –

+0

你是最受歡迎的 – Mostafiz

相關問題