Home > OS >  My MySQL Stored Procedure have an error in line: cmd.ExecuteNonQuery()
My MySQL Stored Procedure have an error in line: cmd.ExecuteNonQuery()

Time:07-18

  1. I have here my stored procedure "sp_ProductAdjustment"
CREATE PROCEDURE sp_ProductAdjustment 
(IN `_product_code` varchar(35), IN `_adjusted_qty` int, 
IN `_stock_in_out` char(3), IN `_status` varchar(10))
BEGIN
   ***some codes here***
END
  1. then my c# code calling my stored procs
    public bool ProductAdjustment(string prodcode, int qty,string in_out,string status)
    {
            conn = Database.getConnection();
            try
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "sp_ProductAdjustment";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("_product_code", MySqlDbType.VarChar).Value = prodcode;
                cmd.Parameters["_product_code"].Direction = ParameterDirection.Input;

                cmd.Parameters.Add("_adjusted_qty", MySqlDbType.Int32).Value = qty;
                cmd.Parameters["_adjusted_qty"].Direction = ParameterDirection.Input;

                cmd.Parameters.Add("_stock_in_out", MySqlDbType.VarChar).Value = in_out;
                cmd.Parameters["_stock_in_out"].Direction = ParameterDirection.Input;

                cmd.Parameters.Add("_status", MySqlDbType.VarChar).Value = status;
                cmd.Parameters["_status"].Direction = ParameterDirection.Input;

                if (cmd.ExecuteNonQuery() == 1) **error on this line**
                {
                    return true;
                }
                else
                {
                    return false;
                }
                
            }catch(MySqlException ex)
            {
                MessageBox.Show("Error "   ex.Number   " has occurred: "   ex.Message);
               
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
  1. my codes calling ProductAdjustment() method
string in_out = radioButton1.Checked ? "IN" : "OUT";
if (prod.ProductAdjustment(txtproductCode.Text, Convert.ToInt32(numericUpDown_adjust.Value.ToString()), in_out, comboBox_reasons.Text))
{
   MessageBox.Show("Product Adjusted");
}
  1. then my error: System.NotSupportedException: 'Character set 'utf8mb3' is not supported by .Net Framework.'

CodePudding user response:

i'm just trying anything until i got the correct solution for calling a Stored Procedure. here's my cents of advice:

cmd.Connection = conn;
cmd.CommandText = "call sp_ProductAdjustment(@product_code, @adjusted_qty, @stock_in_out, @status)";
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add("@product_code", MySqlDbType.VarChar).Value = prodcode;
cmd.Parameters["@product_code"].Direction = ParameterDirection.Input;

cmd.Parameters.Add("@adjusted_qty", MySqlDbType.Int32).Value = qty;
cmd.Parameters["@adjusted_qty"].Direction = ParameterDirection.Input;

cmd.Parameters.Add("@stock_in_out", MySqlDbType.VarChar).Value = in_out;
cmd.Parameters["@stock_in_out"].Direction = ParameterDirection.Input;

cmd.Parameters.Add("@status", MySqlDbType.VarChar).Value = status;
cmd.Parameters["@status"].Direction = ParameterDirection.Input;
  1. First, Notice how I change the CommandType from CommandType.StoredProcedure to CommandType.Text

  2. Next is the text. instead of just calling the stored procs name. I write the whole command of calling the stored procs. from: cmd.CommandText = "sp_ProductAdjustment"; to: cmd.CommandText = "call sp_ProductAdjustment(@product_code, @adjusted_qty, @stock_in_out, @status)";

  3. Then, I named my own parameters inside the sp_ProductAdjustment(parameters here)

  4. Lastly, I don't know why the tutorials online did not work for me, but luckily I still figure out the correct solutions.

  • Related