C# Conversion Error when trying to save to SQL Server database

Asked

Viewed 44 times

1

I’m new to C# and I’m taking a beating to record texbox data in SQL Server inserir a descrição da imagem aqui

private void Btn_salvar_Click(object sender, EventArgs e)
                {

                using (SqlConnection con = new SqlConnection(strConexao))
                {
                    string sql = @"insert into tb_Produtos (descprod, ean, precovenda, precocusto, codforn) values 
                                    (@DPROD,@EAN,@PRECO,@CUSTO,@FORN)";

                    con.Open();

                    using (SqlCommand cmd = new SqlCommand(sql, con))

                    {
                        cmd.Parameters.Add("@DPROD",SqlDbType.VarChar).Value=txtdesc;
                        cmd.Parameters.Add("@EAN",SqlDbType.VarChar).Value=txtean.Text;
                        cmd.Parameters.Add("@PRECO", SqlDbType.Float).Value = txtpreco.Text;
                        cmd.Parameters.Add("@CUSTO",SqlDbType.VarChar).Value= txtprecusto.Text;
                        cmd.Parameters.Add("@FORN",SqlDbType.Int).Value=cbfor.Text;

                        int retorno = cmd.ExecuteNonQuery();
                        if (retorno == 1)
                        {
                            MessageBox.Show("Produto Gravado com sucesso!");

                        }
                        else
                        {
                            MessageBox.Show("Erro ao gravar.");
                        }

                    } 
                }
  • 1

    that txtdesc there shouldn’t be with. text either?

1 answer

5


Your first parameter failed to take the Text property this way:

cmd.Parameters.Add("@DPROD",SqlDbType.VarChar).Value=txtdesc.Text;
  • Show, thank you!!! Can you tell how to pass the values of a combobox coming from a foreign key using the same method I’m trying up there?

Browser other questions tagged

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