Field rounding value in sql write with c#

Asked

Viewed 48 times

0

I have a problem to record a decimal field(18,2) in the sql database, I am passing the value of 11.5, and this recording in the database 12,00. I am working in Decimal fields(18,2). How can I solve the problem.

Follow the imagesinserir a descrição da imagem aqui

inserir a descrição da imagem aqui

follows my code

 private void btn_xml_Click(object sender, EventArgs e)
    {
        if (txt_chave.Text != "")
        {
            DateTime emissao = new DateTime(2018, 7, 24, 00, 00, 00, 0000);
            DateTime saida = new DateTime(2018, 7, 24, 00, 00, 00, 0000);
            string.Format("{0:dd/MM/yyyy}", emissao);
            string.Format("{0:dd/MM/yyyy}", saida);

            DataSet ds = new DataSet();

            ds.ReadXml(@"C:\Xml_Entrada\" + txt_chave.Text + ".xml");

            txt_fornecedor.Text = ds.Tables["emit"].Rows[0]["xNome"].ToString();
            txt_cnpj.Text = ds.Tables["emit"].Rows[0]["CNPJ"].ToString();
            txt_nota.Text = ds.Tables["ide"].Rows[0]["nNF"].ToString();
            txt_ie.Text = ds.Tables["emit"].Rows[0]["IE"].ToString();
            emissao = Convert.ToDateTime(ds.Tables["ide"].Rows[0]["dhEmi"]);
            saida = Convert.ToDateTime(ds.Tables["ide"].Rows[0]["dhSaiEnt"]);

            txt_emissao.Text = Convert.ToString(emissao);
            txt_saida.Text = Convert.ToString(saida);

            string FileName = @"C:\Xml_Entrada\" + txt_chave.Text + ".xml";
            List<ClasseItensXml> ListaItens = new List<ClasseItensXml>();
            XmlDocument doc = new XmlDocument();
            doc.Load(FileName);
            var proditens = doc.GetElementsByTagName("prod");

            foreach (XmlElement nodo in proditens)
            {
                ListaItens.Add(
                     new ClasseItensXml()
                     {
                         NomeProduto = nodo.GetElementsByTagName("xProd")[0].InnerText.Trim(),
                         QuantidadeComercializada = Convert.ToDecimal(nodo.GetElementsByTagName("qCom")[0].InnerText.Trim(), System.Globalization.CultureInfo.InvariantCulture),
                         UnidadeMedida = nodo.GetElementsByTagName("uTrib")[0].InnerText.Trim(),
                         PedidoCompra = nodo.GetElementsByTagName("xPed")[0].InnerText.Trim()
                     });
            }

            dgw_Xml.DataSource = ListaItens;

            DataGridViewComboBoxColumn TipoEmabalagem = new DataGridViewComboBoxColumn();
            TipoEmabalagem.HeaderText = "TIPO EMBALAGEM";
            TipoEmabalagem.ReadOnly = false;
            TipoEmabalagem.DisplayIndex = 10;
            TipoEmabalagem.Sorted = true;
            dgw_Xml.Columns.Add(TipoEmabalagem);

            var embalagem = new ArrayList();
            embalagem.Add("SACARIA");
            embalagem.Add("BOMBONA");
            embalagem.Add("TAMBOR");
            embalagem.Add("TAMBORETE");
            embalagem.Add("BALDE");
            embalagem.Add("BARRICA DE PAPELAO");
            embalagem.Add("CAIXA");
            embalagem.Add("FRASCO");
            embalagem.Add("PACOTE");
            TipoEmabalagem.Items.AddRange(embalagem.ToArray());

            DataGridViewComboBoxColumn Rotulo = new DataGridViewComboBoxColumn();
            Rotulo.HeaderText = "RÓTULOS";
            Rotulo.ReadOnly = false;
            Rotulo.DisplayIndex = 11;
            Rotulo.Sorted = true;
            dgw_Xml.Columns.Add(Rotulo);

            var rotulo = new ArrayList();
            rotulo.Add("SIM-OK");
            rotulo.Add("NAO-OK");
            Rotulo.Items.AddRange(rotulo.ToArray());

            DataGridViewComboBoxColumn Datas = new DataGridViewComboBoxColumn();
            Datas.HeaderText = "DATAS";
            Datas.ReadOnly = false;
            Datas.DisplayIndex = 12;
            Datas.Sorted = true;
            dgw_Xml.Columns.Add(Datas);

            var datas = new ArrayList();
            datas.Add("SIM-OK");
            datas.Add("NAO-OK");
            Datas.Items.AddRange(datas.ToArray());

            DataGridViewComboBoxColumn Lacres = new DataGridViewComboBoxColumn();
            Lacres.HeaderText = "LACRES";
            Lacres.ReadOnly = false;
            Lacres.DisplayIndex = 13;
            Lacres.Sorted = true;
            dgw_Xml.Columns.Add(Lacres);

            var lacres = new ArrayList();
            lacres.Add("SIM-OK");
            lacres.Add("NAO-OK");
            Lacres.Items.AddRange(lacres.ToArray());

            DataGridViewComboBoxColumn Avarias = new DataGridViewComboBoxColumn();
            Avarias.HeaderText = "AVARIAS";
            Avarias.ReadOnly = false;
            Avarias.DisplayIndex = 14;
            Avarias.Sorted = true;
            dgw_Xml.Columns.Add(Avarias);

            var avarias = new ArrayList();
            avarias.Add("SIM-OK");
            avarias.Add("NAO-OK");
            Avarias.Items.AddRange(avarias.ToArray());

            dgw_Xml.Columns["PesoBruto"].DefaultCellStyle.Format = "N3";
            dgw_Xml.Columns["QtdaEmbalagem"].DefaultCellStyle.Format = "N3";
            dgw_Xml.Columns["ConferenciaPB"].DefaultCellStyle.Format = "N3";
            dgw_Xml.Columns["ConferenciaPL"].DefaultCellStyle.Format = "N3";
            dgw_Xml.Columns["QuantidadeComercializada"].DefaultCellStyle.Format = "N3";

            dgw_Xml.Columns["DataFabricacao"].DefaultCellStyle.Format = "d";
            dgw_Xml.Columns["DataValidade"].DefaultCellStyle.Format = "d";

            dgw_Xml.Columns["DataFabricacao"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["DataValidade"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["PesoBruto"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["QtdaEmbalagem"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["ConferenciaPB"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["ConferenciaPL"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["QuantidadeComercializada"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["UnidadeMedida"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgw_Xml.Columns["PedidoCompra"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dgw_Xml.Columns["NomeProduto"].HeaderText = "DESCRIÇÃO";
            dgw_Xml.Columns["LoteFornecedor"].HeaderText = "LOTE";
            dgw_Xml.Columns["DataFabricacao"].HeaderText = "FABRICAÇÃO";
            dgw_Xml.Columns["DataValidade"].HeaderText = "VALIDADE";
            dgw_Xml.Columns["QuantidadeComercializada"].HeaderText = "PESO LIQ.";
            dgw_Xml.Columns["PesoBruto"].HeaderText = "PESO BRUTO";
            dgw_Xml.Columns["QtdaEmbalagem"].HeaderText = "QTDA. EMB.";
            dgw_Xml.Columns["ConferenciaPB"].HeaderText = "CONFERENCIA P.B";
            dgw_Xml.Columns["ConferenciaPL"].HeaderText = "CONFERENCIA P.L";
            dgw_Xml.Columns["UnidadeMedida"].HeaderText = "UNID. MEDIDA";
            dgw_Xml.Columns["PedidoCompra"].HeaderText = "PED. COMPRA";

            dgw_Xml.Columns["NomeProduto"].DisplayIndex = 0;
            dgw_Xml.Columns["LoteFornecedor"].DisplayIndex = 1;
            dgw_Xml.Columns["DataFabricacao"].DisplayIndex = 2;
            dgw_Xml.Columns["DataValidade"].DisplayIndex = 3;
            dgw_Xml.Columns["QuantidadeComercializada"].DisplayIndex = 4;
            dgw_Xml.Columns["UnidadeMedida"].DisplayIndex = 5;
            dgw_Xml.Columns["PesoBruto"].DisplayIndex = 6;
            dgw_Xml.Columns["QtdaEmbalagem"].DisplayIndex = 7;
            dgw_Xml.Columns["ConferenciaPB"].DisplayIndex = 9;
            dgw_Xml.Columns["ConferenciaPL"].DisplayIndex = 8;
            dgw_Xml.Columns["PedidoCompra"].DisplayIndex = 15;

            dgw_Xml.Columns["NomeProduto"].ReadOnly = true;
            dgw_Xml.Columns["LoteFornecedor"].ReadOnly = false;
            dgw_Xml.Columns["DataFabricacao"].ReadOnly = false;
            dgw_Xml.Columns["DataValidade"].ReadOnly = false;
            dgw_Xml.Columns["QuantidadeComercializada"].ReadOnly = true;
            dgw_Xml.Columns["PesoBruto"].ReadOnly = false;
            dgw_Xml.Columns["QtdaEmbalagem"].ReadOnly = false;
            dgw_Xml.Columns["ConferenciaPB"].ReadOnly = true;
            dgw_Xml.Columns["ConferenciaPL"].ReadOnly = true;
            dgw_Xml.Columns["UnidadeMedida"].ReadOnly = true;
            dgw_Xml.Columns["PedidoCompra"].ReadOnly = true;
        }
        else
        {
            System.Windows.MessageBox.Show("Não foi digitado nenhuma chave de acesso ou a chave de acesso digitada não existe no caminho C:\\Xml_Entrada, verifique ");
            return;
        }

    }
    private void dgw_Xml_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        foreach (DataGridViewRow row in dgw_Xml.Rows)
        {
            if (e.ColumnIndex == dgw_Xml.Columns["QtdaEmbalagem"].Index)
            {
                dgw_Xml.CurrentRow.Cells["ConferenciaPB"].Value = ConferirPB();
                dgw_Xml.CurrentRow.Cells["ConferenciaPL"].Value = ConferirPL();
            }
        }
    }
    private decimal ConferirPB()
    {
        decimal pesoBruto = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["PesoBruto"].Value);
        decimal qtdaEmbalagem = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["QtdaEmbalagem"].Value);
        return pesoBruto / qtdaEmbalagem;
    }
    private decimal ConferirPL()
    {
        decimal pesoLiquido = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["QuantidadeComercializada"].Value);
        decimal qtdaEmbalagem = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["QtdaEmbalagem"].Value);
        return pesoLiquido / qtdaEmbalagem;
    }
    private void frmXmlFornecedorPS_Load(object sender, EventArgs e)
    {

    }
    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void btn_gravar_Click(object sender, EventArgs e)
    {
        SqlCommand Inserinseq = new SqlCommand("usp_insericklistsequencial", conexaoBDUSUARIOS(true));
        Inserinseq.Parameters.AddWithValue("@N_Seq", this.txt_cheklist.Text);
        Inserinseq.CommandType = CommandType.StoredProcedure;
        //Inserinseq.ExecuteNonQuery();


        SqlCommand chkrecebimento = new SqlCommand("usp_ckhrecebimento", conexaoBDUSUARIOS(true));
        chkrecebimento.CommandType = CommandType.StoredProcedure;

        chkrecebimento.Parameters.Add(new SqlParameter("@ID_CKCLIST",   this.txt_cheklist.Text));
        chkrecebimento.Parameters.Add(new SqlParameter("@CHV_NFE",      this.txt_chave.Text));
        chkrecebimento.Parameters.Add(new SqlParameter("@N_FONEC",      this.txt_fornecedor.Text));
        chkrecebimento.Parameters.Add(new SqlParameter("@N_NOTA",       this.txt_nota.Text));
        chkrecebimento.Parameters.Add(new SqlParameter("@N_CNPJ",       this.txt_cnpj.Text.Replace(".", "").Replace("/", "").Replace("-", "")));
        chkrecebimento.Parameters.Add(new SqlParameter("@N_IE",         this.txt_ie.Text.Replace(".", "")));
        chkrecebimento.Parameters.Add(new SqlParameter("DT_EMISSAO",    this.txt_emissao.Text));
        chkrecebimento.Parameters.Add(new SqlParameter("@DT_SAIDA",     this.txt_saida.Text));
        chkrecebimento.Parameters.Add(new SqlParameter("@DT_RECEBIM",   this.txt_recebimento.Text));
        //chkrecebimento.ExecuteNonQuery();

        SqlCommand chkitens = new SqlCommand("usp_chkitens", conexaoBDUSUARIOS(true));
        chkitens.CommandType = CommandType.StoredProcedure;

        for (int i = 0; i < dgw_Xml.Rows.Count; i++)
        {
            chkitens.Parameters.Clear();
            chkitens.Parameters.Add(new SqlParameter("@ID_CKCLIST",       this.txt_cheklist.Text));
            chkitens.Parameters.Add(new SqlParameter("@CK_DESCRI",        this.dgw_Xml.Rows[i].Cells[0].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_LOTE",          this.dgw_Xml.Rows[i].Cells[7].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_FABRIC",        this.dgw_Xml.Rows[i].Cells[8].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_VALID",         this.dgw_Xml.Rows[i].Cells[9].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_PESNFE",        this.dgw_Xml.Rows[i].Cells[1].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_UNIMED",        this.dgw_Xml.Rows[i].Cells[2].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_PESB",          this.dgw_Xml.Rows[i].Cells[3].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_QTDEMB",        this.dgw_Xml.Rows[i].Cells[6].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_CONFPL",        this.dgw_Xml.Rows[i].Cells[5].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_CONFPB",        this.dgw_Xml.Rows[i].Cells[4].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_TPEMB",         this.dgw_Xml.Rows[i].Cells[11].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_ROTULO",        this.dgw_Xml.Rows[i].Cells[12].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_DATAS",         this.dgw_Xml.Rows[i].Cells[13].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_LACRES",        this.dgw_Xml.Rows[i].Cells[14].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_AVARIAS",       this.dgw_Xml.Rows[i].Cells[15].Value));
            chkitens.Parameters.Add(new SqlParameter("@CK_PEDCOMP",       this.dgw_Xml.Rows[i].Cells[10].Value));
            chkitens.ExecuteNonQuery();
        }

    }
  • 1

    Already tried to convert the value to decimal, float or double when you add the parameter this.dgw_Xml.Rows[i]. Cells[4]. Value?

  • 2

    What type is defined in stored

  • Jorge thank you very much, my past was only as Decimal and not as Decimal(18,2). solved my problem.

  • 2

    You need to set when creating the parameter Precision=18 and Scale=2. Look for the maker on SqlParameter that accepts these parameters

No answers

Browser other questions tagged

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