简体   繁体   中英

Deleting data using Stored Procedure in MySQL

I'm Have a Problem in deleting my data in my database. When I Click the delete button, There's an error pop out saying that Unable to cast of Type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'. Can anyone solve this? its my first time to encounter this error.

The code below is calling the stored Procedure in my Database.

Here's the code I use

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();

    }

How can you assign a textbox to an integer parameter?

Provide as Convert.ToInt32(ProdNo.Text)

You should use converter Convert.ToInt32(ProdNo.Text) if not work use this Convert.ToDecimal(ProdNo.Text)

So, The final code will be

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();

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM