Inserting double type value into mysql database

Asked

Viewed 652 times

0

I have a Windowsform with a value field and I need to enter its value in the database.

I need some example of how to do this?

File that queries

//Cadastrar Gastos
        public void cadastroGasto(Gasto gasto)
        {
            try
            {
                string inserir = "INSERT INTO gasto(descricao,valor,funcionario_id)value('"+ gasto.Descricao +"', '"+ gasto.Valor +"', '"+ gasto.Funcionario_id +"')";

                conexao = new MySqlConnection(caminho);
                MySqlCommand comandos = new MySqlCommand(inserir, conexao);

                conexao.Open();
                comandos.ExecuteNonQuery();
                conexao.Close();
            }

            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                MessageBox.Show("Cadastro efetuado com sucesso!",
                    "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                conexao.Close();
            }
        }

Here is the windowsForm code

private void btnSalvar_Click(object sender, EventArgs e)
        {
            Classes.Gasto gasto = new Classes.Gasto();

            Classes.DBconect con = new Classes.DBconect();

            gasto.Descricao = txtDescricao.Text;
            string valor = txtValor.Text;
            Decimal dvalor = Convert.ToDecimal(valor, System.Globalization.CultureInfo.GetCultureInfo("pt-BR"));

            gasto.Valor = dvalor;

            gasto.Funcionario_id = int.Parse(cmbFuncionario.SelectedValue.ToString());

            this.ValidarCampos();

            //verifica se as validações retornaram true
            if (this.ValidarCampos() == true)
            {
                //Insere os dados no bd
                con.cadastroGasto(gasto);
             }
      }

This second block of code is not working well inserting txtValor

  • How is your database?

  • mysql with double(8.2 type price field)

  • You already have some code ready to put in your question?

  • 2

    Yes, it does. /s

No answers

Browser other questions tagged

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