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?
– novic
I already see some problems.
ArrayList
is deprecated, prefer to useList
. 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 thisArrayList
. Can’t usefloat
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.– Maniero
Thanks for the days
– Paulo.Pasiam
@Paul.Pasiam managed to solve his problem?
– Denis