Jump to content
marioverdini

casas decimais

Recommended Posts

marioverdini

estou tendo um probleminha no meu codigo
nos campos ( total e sub total) estou tentando multiplicar o resultado esta sendo inteiro 
EX: 1.02 x 3 = 3.00 ao inves de 3.06 
alguem poderia me dar uma ajuda 
se esse trecho do mcodigo nao for claro para os amigo do forum eu mando o codigo inteiro do formulario

Public Class frmPurchase
    Private Sub auto()
        Try
            Dim Num As Integer = 0
            con = New SqlConnection(cs)
            con.Open()
            Dim Sql As String = ("SELECT MAX(PI_ID) FROM Purchase")
            cmd = New SqlCommand(Sql)
            cmd.Connection = con
            If (IsDBNull(cmd.ExecuteScalar)) Then
                Num = 1
                txtID.Text = Num.ToString
            Else
                Num = cmd.ExecuteScalar + 1
                txtID.Text = Num.ToString
            End If
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub 

Edited by marioverdini

Share this post


Link to post
Share on other sites
antseq

Viva,

do que vejo no código acima, o mesmo parece ser para ir buscar o "próximo" número (1 ou MAX+1) e não tem nada que ver com o Total e Subtotal onde tem o problema.

consegue mostrar o seu código na parte do calculo do Total e SubTotal?

cps,

 

Share this post


Link to post
Share on other sites
marioverdini

entao vou colocar o codigo todo pq ate eu ja estou perdido rsrsrsrs
quem puder me ajudar agradeço
-------------------------------------------------------------------------------------------------

Imports System.Data.SqlClient
Imports System.IO

Public Class frmPurchase
    Private Sub auto()
        Try
            Dim Num As integer = 0
            con = New SqlConnection(cs)
            con.Open()
            Dim Sql As String = ("SELECT MAX(PI_ID) FROM Purchase")
            cmd = New SqlCommand(Sql)
            cmd.Connection = con
            If (IsDBNull(cmd.ExecuteScalar)) Then
                Num = 1
                txtID.Text = Num.ToString
            Else
                Num = cmd.ExecuteScalar + 1
                txtID.Text = Num.ToString
            End If
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        If Len(Trim(cmbPurchaseType.Text)) = 0 Then
            MessageBox.Show("Please select purchase type", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            cmbPurchaseType.Focus()
            Exit Sub
        End If
        If Len(Trim(cmbCompany.Text)) = 0 Then
            MessageBox.Show("Please select company", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            cmbCompany.Focus()
            Exit Sub
        End If
        If DataGridView1.Rows.Count = 0 Then
            MessageBox.Show("Sorry no product info added to grid", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If
        If Len(Trim(txtTaxPer.Text)) = 0 Then
            MessageBox.Show("Please enter tax %", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtTaxPer.Focus()
            Exit Sub
        End If
        Try
            con = New SqlConnection(cs)
            con.Open()
            Dim cb As String = "insert into Purchase(PI_ID, Date, PurchaseType, Company, SubTotal, TaxPer, TaxAmount, GrandTotal, Remarks) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9)"
            cmd = New SqlCommand(cb)
            cmd.Parameters.AddWithValue("@d1", txtID.Text)
            cmd.Parameters.AddWithValue("@d2", Convert.ToDateTime(dtpDate.Value.Date))
            cmd.Parameters.AddWithValue("@d3", cmbPurchaseType.Text)
            cmd.Parameters.AddWithValue("@d4", cmbCompany.Text)
            cmd.Parameters.AddWithValue("@d5", txtSubTotal.Text)
            cmd.Parameters.AddWithValue("@d6", txtTaxPer.Text)
            cmd.Parameters.AddWithValue("@d7", txtTaxAmount.Text)
            cmd.Parameters.AddWithValue("@d8", txtGrandTotal.Text)
            cmd.Parameters.AddWithValue("@d9", txtRemarks.Text)
            cmd.Connection = con
            cmd.ExecuteNonQuery()
            con.Close()
            con = New SqlConnection(cs)
            con.Open()
            Dim cb1 As String = "insert into Purchase_Product(PurchaseID,ProductID,Quantity,Price,TotalAmount) VALUES (" & txtID.Text & ",@d1,@d2,@d3,@d4)"
            cmd = New SqlCommand(cb1)
            cmd.Connection = con
            ' Prepare o comando para execução repetida
            cmd.Prepare()
            ' Dados a serem inseridos
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then
                    cmd.Parameters.AddWithValue("@d1", row.Cells(0).Value)
                    cmd.Parameters.AddWithValue("@d2", row.Cells(3).Value)
                    cmd.Parameters.AddWithValue("@d3", row.Cells(4).Value)
                    cmd.Parameters.AddWithValue("@d4", row.Cells(5).Value)
                    cmd.ExecuteNonQuery()
                    cmd.Parameters.Clear()
                End If
            Next
            con.Close()
            LogFunc(lblUser.Text, "added the new purchase having purchase ID '" & txtID.Text & "'")
            MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
            btnSave.Enabled = False
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        frmProductRecord.lblSet.Text = "Purchase"
        frmProductRecord.Reset()
        frmProductRecord.ShowDialog()
    End Sub
    Sub Reset()
        txtSubTotal.Text = ""
        txtPricePerQty.Text = ""
        txtProductCode.Text = ""
        txtProductName.Text = ""
        txtQty.Text = ""
        txtRemarks.Text = ""
        cmbCompany.Text = ""
        cmbPurchaseType.SelectedIndex = -1
        txtTotalAmount.Text = ""
        txtTaxPer.Text = ""
        txtTotalAmount.Text = ""
        dtpDate.Text = Now
        DataGridView1.Rows.Clear()
        btnSave.Enabled = True
        btnUpdate.Enabled = False
        btnRemove.Enabled = False
        txtGrandTotal.Text = ""
        dtpDate.Enabled = True
        DataGridView1.Enabled = True
        btnAdd.Enabled = True
        auto()
    End Sub
    Private Sub btnNew_Click(sender As System.Object, e As System.EventArgs) Handles btnNew.Click
        Reset()
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        Try
            If txtProductCode.Text = "" Then
                MessageBox.Show("Please retrieve product code", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtProductCode.Focus()
                Exit Sub
            End If
            If txtQty.Text = "" Then
                MessageBox.Show("Please enter quantity", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtQty.Focus()
                Exit Sub
            End If
            If txtQty.Text = 0 Then
                MessageBox.Show("Quantity can not be zero", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtQty.Focus()
                Exit Sub
            End If
            If txtPricePerQty.Text = "" Then
                MessageBox.Show("Please enter price per qty.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtPricePerQty.Focus()
                Exit Sub
            End If
            If DataGridView1.Rows.Count = 0 Then
                DataGridView1.Rows.Add(txtProductID.Text, txtProductCode.Text, txtProductName.Text, txtQty.Text, Val(txtPricePerQty.Text), Val(txtTotalAmount.Text))
                Dim k As Double = 0
                k = SubTotal()
                k = Math.Round(k, 2)
                txtSubTotal.Text = k
                Clear()
                Exit Sub
            End If
            For Each r As DataGridViewRow In Me.DataGridView1.Rows
                If r.Cells(1).Value = txtProductCode.Text Then
                    r.Cells(0).Value = txtProductID.Text
                    r.Cells(1).Value = txtProductCode.Text
                    r.Cells(2).Value = txtProductName.Text
                    r.Cells(3).Value = Val(r.Cells(3).Value) + Val(txtQty.Text)
                    r.Cells(4).Value = Val(txtPricePerQty.Text)
                    r.Cells(5).Value = Val(r.Cells(5).Value) + Val(txtTotalAmount.Text)
                    Dim i As Double = 0
                    i = SubTotal()
                    i = Math.Round(i, 2)
                    txtSubTotal.Text = i
                    Clear()
                    Exit Sub
                End If
            Next
            DataGridView1.Rows.Add(txtProductID.Text, txtProductCode.Text, txtProductName.Text, txtQty.Text, Val(txtPricePerQty.Text), Val(txtTotalAmount.Text))
            Dim j As Double = 0
            j = SubTotal()
            j = Math.Round(j, 2)
            txtSubTotal.Text = j
            Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Function SubTotal() As Double
        Dim sum As Double = 0
        Try
            For Each r As DataGridViewRow In Me.DataGridView1.Rows
                sum = sum + r.Cells(5).Value
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return sum
    End Function
    Sub Clear()
        txtProductCode.Text = ""
        txtProductName.Text = ""
        txtQty.Text = ""
        txtPricePerQty.Text = ""
        txtTotalAmount.Text = ""
    End Sub

    Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
        Try
            For Each row As DataGridViewRow In DataGridView1.SelectedRows
                DataGridView1.Rows.Remove(row)
            Next
            Dim k As Double = 0
            k = SubTotal()
            k = Math.Round(k, 2)
            txtSubTotal.Text = k
            Compute()
            btnRemove.Enabled = False
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Sub Compute()
        Dim i As Double = 0
        Dim j As Double = 0
        j = Val((Val(txtSubTotal.Text) * Val(txtTaxPer.Text)) / 100)
        j = Math.Round(j, 2)
        txtTaxAmount.Text = j
        i = Val(txtSubTotal.Text) + Val(txtTaxAmount.Text)
        i = Math.Round(i, 2)
        txtGrandTotal.Text = i
    End Sub

    Private Sub DataGridView1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
        If DataGridView1.Rows.Count > 0 Then
            btnRemove.Enabled = True
        End If
    End Sub

    Private Sub DataGridView1_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString()
        Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
        If DataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then
            DataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20))
        End If
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))

    End Sub
    Sub fillCompany()
        Try
            con = New SqlConnection(cs)
            con.Open()
            adp = New SqlDataAdapter()
            adp.SelectCommand = New SqlCommand("SELECT distinct RTRIM(CompanyName) FROM Company order by 1", con)
            ds = New DataSet("ds")
            adp.Fill(ds)
            dtable = ds.Tables(0)
            cmbCompany.Items.Clear()
            For Each drow As DataRow In dtable.Rows
                cmbCompany.Items.Add(drow(0).ToString())
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub frmStock_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        fillCompany()
    End Sub

    Private Sub txtPricePerQty_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtPricePerQty.TextChanged
        Dim i As Double = 0
        i = CDbl(Val(txtQty.Text) * Val(txtPricePerQty.Text))
        i = Math.Round(i, 2)
        txtTotalAmount.Text = i
    End Sub


    Private Sub txtQty_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtQty.TextChanged
        Dim i As Double = 0
        i = CDbl(Val(txtQty.Text) * Val(txtPricePerQty.Text))
        i = Math.Round(i, 2)
        txtTotalAmount.Text = i
    End Sub

    Private Sub txtQty_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtQty.KeyPress
        If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPricePerQty_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtPricePerQty.KeyPress
        Dim keyChar = e.KeyChar

        If Char.IsControl(keyChar) Then
            'Permitir todos os caracteres de controle.
        ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
            Dim text = Me.txtPricePerQty.Text
            Dim selectionStart = Me.txtPricePerQty.SelectionStart
            Dim selectionLength = Me.txtPricePerQty.SelectionLength

            text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)

            If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
                'Rejeitar um numero inteiro com mais de 16 dígitos.
                e.Handled = True
            ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
                'Rejeitar um número real com duas casas decimais.
                e.Handled = False
            End If
        Else
            'Rejeitar todo tipo de caracteres
            e.Handled = True
        End If
    End Sub

    Private Sub txtTotalPayment_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTaxPer.TextChanged
        Compute()
    End Sub

    Private Sub txtTotalPayment_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles txtTaxPer.Validating
        If Val(txtTaxPer.Text) > Val(txtSubTotal.Text) Then
            MessageBox.Show("Total payment can not be more than grand total", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Exit Sub
    End Sub

    Private Sub btnGetData_Click(sender As System.Object, e As System.EventArgs) Handles btnGetData.Click
        frmPurchaseRecord.lblSet.Text = "Purchase"
        frmPurchaseRecord.Reset()
        frmPurchaseRecord.ShowDialog()
    End Sub

    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click
        If Len(Trim(cmbPurchaseType.Text)) = 0 Then
            MessageBox.Show("Please select purchase type", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            cmbPurchaseType.Focus()
            Exit Sub
        End If
        If Len(Trim(cmbCompany.Text)) = 0 Then
            MessageBox.Show("Please select company", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            cmbCompany.Focus()
            Exit Sub
        End If
        If DataGridView1.Rows.Count = 0 Then
            MessageBox.Show("Sorry no product info added to grid", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If
        If Len(Trim(txtTaxPer.Text)) = 0 Then
            MessageBox.Show("Please enter tax %", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtTaxPer.Focus()
            Exit Sub
        End If
        Try
            con = New SqlConnection(cs)
            con.Open()
            Dim cb As String = "Update Purchase set Date=@d2, PurchaseType=@d3, Company=@d4, SubTotal=@d5, TaxPer=@d6, TaxAmount=@d7, GrandTotal=@d8, Remarks=@d9 where PI_ID=@d1"
            cmd = New SqlCommand(cb)
            cmd.Parameters.AddWithValue("@d1", txtID.Text)
            cmd.Parameters.AddWithValue("@d2", Convert.ToDateTime(dtpDate.Value.Date))
            cmd.Parameters.AddWithValue("@d3", cmbPurchaseType.Text)
            cmd.Parameters.AddWithValue("@d4", cmbCompany.Text)
            cmd.Parameters.AddWithValue("@d5", txtSubTotal.Text)
            cmd.Parameters.AddWithValue("@d6", txtTaxPer.Text)
            cmd.Parameters.AddWithValue("@d7", txtTaxAmount.Text)
            cmd.Parameters.AddWithValue("@d8", txtGrandTotal.Text)
            cmd.Parameters.AddWithValue("@d9", txtRemarks.Text)
            cmd.Connection = con
            cmd.ExecuteNonQuery()
            con.Close()
            con = New SqlConnection(cs)
            con.Open()
            Dim cq As String = "delete from Purchase_Product where PurchaseID=@d1"
            cmd = New SqlCommand(cq)
            cmd.Parameters.AddWithValue("@d1", txtID.Text)
            cmd.Connection = con
            cmd.ExecuteNonQuery()
            con.Close()
            con = New SqlConnection(cs)
            con.Open()
    Dim cb1 As String = "insert into Purchase_Product(PurchaseID,ProductID,Quantity,Price,TotalAmount) VALUES (" & txtID.Text & ",@d1,@d2,@d3,@d4)"
            cmd = New SqlCommand(cb1)
            cmd.Connection = con
            ' Prepare o comando para execução repetida
            cmd.Prepare()
            ' Dados a serem inseridos
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then
                    cmd.Parameters.AddWithValue("@d1", row.Cells(0).Value)
                    cmd.Parameters.AddWithValue("@d2", row.Cells(3).Value)
                    cmd.Parameters.AddWithValue("@d3", row.Cells(4).Value)
                    cmd.Parameters.AddWithValue("@d4", row.Cells(5).Value)
                    cmd.ExecuteNonQuery()
                    cmd.Parameters.Clear()
                End If
            Next
            con.Close()
            LogFunc(lblUser.Text, "updated the new purchase entry having purchase ID '" & txtID.Text & "'")
            MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
            btnSave.Enabled = False
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Try
            If MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                DeleteRecord()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub DeleteRecord()
        Try
            Dim RowsAffected As Integer = 0
           
            con = New SqlConnection(cs)
            con.Open()
            Dim cq As String = "delete from Purchase where PI_ID=@d1"
            cmd = New SqlCommand(cq)
            cmd.Parameters.AddWithValue("@d1", txtID.Text)
            cmd.Connection = con
            RowsAffected = cmd.ExecuteNonQuery()
            If RowsAffected > 0 Then
                LogFunc(lblUser.Text, "deleted the Purchase entry having id='" & txtID.Text & "'")
                MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Reset()
            Else
                MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Reset()
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                con.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub txtSubTotal_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSubTotal.TextChanged
        Compute()
    End Sub


End Class
 

Share this post


Link to post
Share on other sites
antseq

Viva,

VisualBasic não é a minha praia, mas assumindo que como diz os valores decimais "1.02" ficam "inteiros", desconfio um pouco das conversões que faz de "txtValor.Text = decValor" e vice-versa para "decValor = Val(txtValor.Text)".

Se fizer um programa com apenas 3 linhas, consegue testar este caso nomeadamente se decValor = decValor2:

 Dim decValor As Double = 1.02
 txtValor.Text = decValor
 Dim decValor2 As Double = Val(txtValor.Text)

* Mesmo que não seja aqui o problema, dependendo das configurações regionais do computador (texto no ecran "1.02" ou "1,02") que utiliza, pode funcionar bem em alguns casos mas um dia encontra um caso que não funciona.

Seu código também poderia beneficiar de algumas optimizações, não para ficar rápido, mas para ser mais claro na leitura, menos repetições e menos sujeito a erros.

Por exemplo estes 3 casos:

Private Sub btnAdd_Click
 If DataGridView1.Rows.Count = 0
    DataGridView1.Rows.Add(...)
 ...
 For Each r As DataGridViewRow In Me.DataGridView1.Rows...
 ...
 DataGridView1.Rows.Add(...)
 ...

podem ser simplificados em apenas 2:

 Se existe em Me.DataGridView1.Rows :  QTD = QTD + NovaQTD
 Senão Me.DataGridView1.Rows.Add(...)

Tal como este "cálculo" que faz é igual\comum para todos estes 3 casos como também é repetido noutras partes do programa, poderia estar mais "centralizado" ou menos repetido:

 Dim i As Double = 0
 i = SubTotal()
 i = Math.Round(i, 2)
 txtSubTotal.Text = i

cps,


 

Share this post


Link to post
Share on other sites
Mikev

Boas tardes, 

Das próximas vezes tente colocar o código formatado e apenas no sítio onde tem o erro, ou torna-se muito complicado tentar ajudar...

Abraço.


C# 😍

Share this post


Link to post
Share on other sites
marioverdini

Bom Dia Mikev 
tudo bem?
neste caso não houve erro pelo contrario eu apenas estava em duvida nesse trecho e acabei colocando o codigo todo 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.