How to insert float value by arraylist - C#?

Asked

Viewed 114 times

1

Hello, I can not pass a floating value (price) by arraylist to the bank . I wonder if I need to convert before passing or if there is any way to do straight. I’ve looked it up in Stack, but I can’t find anything like it. Follow the code I’m studying :

public bool Insert(ArrayList p_arrInsert)

  {  

vsql = "INSERT INTO tb_produtos ([PROD_NOME],[PROD_TIPO],PROD_DESCRICAO],[PROD_PRECO]) VALUES (@PROD_NOME, @PROD_TIPO, @PROD_DESCRICAO, @PROD_PRECO)";

            SqlCommand objcmd = null;


            if (this.conectar())
            {

                try
                {
                    objcmd = new SqlCommand(vsql, objCon);
                    objcmd.Parameters.Add(new SqlParameter("@PROD_NOME", p_arrInsert[0]));
                    objcmd.Parameters.Add(new SqlParameter("@PROD_TIPO", p_arrInsert[1]));
                    objcmd.Parameters.Add(new SqlParameter("@PROD_DESCRICAO", p_arrInsert[2]));
                    objcmd.Parameters.Add(new SqlParameter("@PROD_PRECO", p_arrInsert[3]));

                    objcmd.ExecuteNonQuery();

                    return true;

                }
                catch (SqlException sqlerr)
                {
                    throw sqlerr;
                }
                finally
                {
                    this.desconectar();
                }
            }
            else
            {
                return false;
            }
        }

I receive the data through a button :

 private void btnCadastrar_Click(object sender, EventArgs e)
        {
            sisDBADM_PRODUTOS obj = new sisDBADM_PRODUTOS();
            ArrayList arr = new ArrayList();
            try
            {
                arr.Add(tbCad_nome.Text);
                arr.Add(cbTipo.Text);
                arr.Add(tbCad_nome.Text);
                arr.Add(tbCad_descricao.Text);
                arr.Add(tbCad_preco.Text);

                if (obj.Insert(arr))
                {
                    MessageBox.Show("Produto cadastrado com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    tbCad_codigo.Text = "";
                    tbCad_nome.Text = "";
                    tbCad_descricao.Text = "";
                    tbCad_preco.Text = "";


                    dgProdutos.DataSource = obj.ListaGrid(); //recarrega o datagrid

                }
                else
                {
                    MessageBox.Show("Erro ao cadastrar o produto", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception erro)
            {
                MessageBox.Show(erro + "Erro Ocorrido!");

            }
        }

I get the error : Error converting nvarchar data type to float....

  • Need conversion ... as is the price value?

  • 1

    I already see some problems. ArrayList is deprecated, prefer to use List. The fact that heterogeneous data comes in a array and not in one class already shows another problem. I actually think that if you did it the right way and you didn’t need this ArrayList. Can’t use float for monetary value: http://answall.com/a/38140/101. You are capturing exception and doing nothing, this is a mistake. If it is to do nothing, do not capture the exception, even if you solve what you want, the system will be generating wrong values, sometimes, which is worse because you think you are right. It’s a serious problem.

  • Thanks for the days

  • @Paul.Pasiam managed to solve his problem?

1 answer

0

I am working with access and save the decimal values in a text column (Examples: 21275 | 47404,95 | 66295,9), I do the processing to display through the string. Format and to manipulate in the system I use a decimal property?.

Another thing you can do is change the popular mode of Parameters:

cmd.Parameters.Add("@StatusId", OleDbType.Integer).Value = ((object)pendencia.StatusId) ?? DBNull.Value;
cmd.Parameters.Add("@FollowUp", OleDbType.Date).Value = (String.IsNullOrEmpty(pendencia.FollowUp) ? DBNull.Value : (((object)pendencia.FollowUp) ?? DBNull.Value));
cmd.Parameters.Add("@DocumentoTipo", OleDbType.VarChar, 255).Value = ((object)pendencia.DocumentoTipo) ?? DBNull.Value;
cmd.Parameters.Add("@IsCritico", OleDbType.Boolean).Value = ((object)pendencia.IsCritico) ?? DBNull.Value;

Browser other questions tagged

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