How to get a Mysql output Parameter via C#

Asked

Viewed 364 times

1

I researched and applied what I saw, but is giving error:

MySqlConnection con = new MySqlConnection(banco.Conexao);
MySqlCommand comando = new MySqlCommand("sp_venda", con);
comando.CommandType = System.Data.CommandType.StoredProcedure;

comando.Parameters.Add("usuario", MySqlDbType.Int32).Value = banco.Usuario;
comando.Parameters.Add("dinheiro", MySqlDbType.Double).Value = banco.formataDinheiro(Venda.Dinheiro);
comando.Parameters.Add("debito", MySqlDbType.Double).Value = banco.formataDinheiro(Venda.Dinheiro);
comando.Parameters.Add("credito", MySqlDbType.Double).Value = banco.formataDinheiro(Venda.Dinheiro);
comando.Parameters.Add("voucher", MySqlDbType.Double).Value = banco.formataDinheiro(Venda.Dinheiro);
comando.Parameters.Add("desconto", MySqlDbType.Double).Value = banco.formataDinheiro(VendaBLL.somaDescontos().ToString());
comando.Parameters.Add("troco", MySqlDbType.Double).Value = banco.formataDinheiro(Venda.Troco);

MySqlParameter paramHorario = new MySqlParameter("horario", MySqlDbType.Time);
paramHorario.Direction = System.Data.ParameterDirection.Output;

comando.Parameters.Add(paramHorario);

con.Open();
comando.ExecuteNonQuery();
Venda.Horario = comando.Parameters["horario"].Value.ToString();
con.Close();

The error occurs on the following line:

comando.ExecuteNonQuery();

The connection opens normally. The error returned is: "Formatexception: Input string was not in an incorrect format." What am I doing wrong?

Edit

 public static string formataDinheiro(string grana)
 {
   if (DetectaTipo(grana) == "null")
      return "null";

 return grana.Replace("R$ ", "").Replace(",", ".");
 }

1 answer

1


It means that some of its parameters are not formatted as it should be. Its function banco.formataDinheiro() is not returning a Double, and yes a String. That’s why the mistake.

To work with money, the ideal is the type Decimal. The Double can bring complications, such as rounding problems.

  • It worked! I didn’t think it could be because while debugging the compiler went through this part quietly. I will follow your advice and change the type to decimal. As soon as I do, mark the answer as correct

  • 1

    This is because the parameters are solved at command runtime. Hence the error is "late".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.