2017-03-17 111 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(); 

    }