2017-03-17 114 views
0

我在刪除數據庫中的數據時遇到問題。當我點擊刪除按鈕時,彈出錯誤提示無法強制類型'System.Windows.Forms.TextBox'轉換爲鍵入'System.IConvertible'。任何人都可以解決這個問題嗎?這是我第一次遇到這個錯誤。使用MySQL中的存儲過程刪除數據

以下代碼在我的數據庫中調用存儲過程

下面的代碼我使用

private void button3_Click(object sender, EventArgs e) 
    { 

     MySqlParameter[] pms = new MySqlParameter[1]; 
     pms[0] = new MySqlParameter("uProdNo", MySqlDbType.Int32); 
     pms[0].Value = ProdNo; 

     MySqlCommand command = new MySqlCommand(); 

     command.Connection = conString; 
     command.CommandType = CommandType.StoredProcedure; 
     command.CommandText = "pos_delete"; 

     command.Parameters.AddRange(pms); 

     conString.Open(); 
     if (command.ExecuteNonQuery() == 1) 
     { 
      dt.Rows.Clear(); 
      MessageBox.Show("Deleted"); 

     } 
     else 
     { 
      MessageBox.Show("Sorry nothing to be deleted"); 
     } 
     conString.Close(); 

    } 
+0

可能是一個錯字:它應該是'ProdNo.Text ',轉換爲數字 – dlatikay

+0

@dlatikay謝謝 –

回答

0

你怎麼可以指定一個文本框整數參數?

提供爲Convert.ToInt32(ProdNo.Text)

0

你應該如用轉換器Convert.ToInt32(ProdNo.Text)不行使用此Convert.ToDecimal(ProdNo.Text)

所以,最後的代碼將是

private void button3_Click(object sender, EventArgs e) 
    { 

     MySqlParameter[] pms = new MySqlParameter[1]; 
     pms[0] = new MySqlParameter("uProdNo", MySqlDbType.Int32); 
     pms[0].Value = Convert.ToInt32(ProdNo.Text); 

     MySqlCommand command = new MySqlCommand(); 

     command.Connection = conString; 
     command.CommandType = CommandType.StoredProcedure; 
     command.CommandText = "pos_delete"; 

     command.Parameters.AddRange(pms); 

     conString.Open(); 
     if (command.ExecuteNonQuery() == 1) 
     { 
      dt.Rows.Clear(); 
      MessageBox.Show("Deleted"); 

     } 
     else 
     { 
      MessageBox.Show("Sorry nothing to be deleted"); 
     } 
     conString.Close(); 

    }