транзакция не обновляет баланс - PullRequest
0 голосов
/ 20 июня 2019

ниже мои коды, которые я пишу для модуля оплаты наличными.после того, как пользователь нажмет сохранение, все записи в datagridview будут затронуты в соответствии с типом столбца, который имеет дебет и кредит, и все записи работают соответственно.Ниже мой код, который сохраняет данные, но не обновляет остатки на счетах.

мне нужно, чтобы данные счетов обновлялись наличными (все работает), а все балансы обновлялись в Плане счетов.

con = New SqlConnection(appx.myconnection)
    con.Open()
    Dim cb As String = "insert into cashpayment(cashpaymentid,date,coa_id2,paymentfrom,coa_id,paymentto,amountdr,remarks,totalamount) Values (@d12,@d13,@d14,@d15,@d16,@d17,@d18,@d21,@d22)"
    cmd = New SqlCommand(cb)
    cmd.Parameters.AddWithValue("@d12", CashPaymentidTextBox.Text)
    cmd.Parameters.AddWithValue("@d13", DateDateTimePicker.Value)
    cmd.Parameters.AddWithValue("@d14", Coa_id2TextBox.Text)
    cmd.Parameters.AddWithValue("@d15", PaymentfromTextBox.Text)
    cmd.Parameters.AddWithValue("@d16", Coa_idTextBox.Text)
    cmd.Parameters.AddWithValue("@d17", PaymenttoTextBox.Text)
    cmd.Parameters.AddWithValue("@d18", AmountdrTextBox.Text)
    cmd.Parameters.AddWithValue("@d21", RemarksTextBox.Text)
    cmd.Parameters.AddWithValue("@d22", TotalamountTextBox.Text)
    cmd.Connection = con
    cmd.ExecuteReader()
    con.Close()
    con = New SqlConnection(appx.myconnection)
    con.Open()
    Dim cb1 As String = "insert into [cashpayment_join](cpvidj,cashpaymentid,date,coafrom,paymentfrom,coato,paymentto,dr,type,description,userid) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11)"
    cmd = New SqlCommand(cb1)
    cmd.Connection = con
    ' Prepare command for repeated execution
    cmd.Prepare()
    ' Data to be inserted
    For Each row As DataGridViewRow In DataGridView1.Rows
        If Not row.IsNewRow Then
            cmd.Parameters.AddWithValue("@d1", row.Cells(0).Value) 'cashpaymentid
            cmd.Parameters.AddWithValue("@d2", row.Cells(1).Value) 'cashpaymentid
            cmd.Parameters.AddWithValue("@d3", row.Cells(2).Value)  'date
            cmd.Parameters.AddWithValue("@d4", row.Cells(3).Value)  'paymentto
            cmd.Parameters.AddWithValue("@d5", row.Cells(4).Value)  'paymentfrom
            cmd.Parameters.AddWithValue("@d6", row.Cells(5).Value)  'dr
            cmd.Parameters.AddWithValue("@d7", row.Cells(6).Value)  'cr
            cmd.Parameters.AddWithValue("@d8", row.Cells(7).Value)  'description
            cmd.Parameters.AddWithValue("@d9", row.Cells(8).Value)  'total
            cmd.Parameters.AddWithValue("@d10", row.Cells(9).Value)  'total
            cmd.Parameters.AddWithValue("@d11", row.Cells(10).Value)  'total
            '      cmd.Parameters.AddWithValue("@d10", row.Cells(9).Value) 'userid
            cmd.ExecuteNonQuery()
            cmd.Parameters.Clear()
        End If
    Next
    con.Close()


    For Each row As DataGridViewRow In DataGridView1.Rows
        '  Using cn As New SqlConnection(appx.myconnection)
        Using cn1 As New SqlConnection(appx.myconnection)
            cn1.Open()
            Using com As New SqlCommand("", cn1)
                For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
                    If Me.DataGridView1.Rows(i).Cells("Type").Value = "Credit" Then
                        com.CommandText = "Update COA set opening_balance = opening_balance - @dra where AccountID=@ida"
                        com.Parameters.AddWithValue("@ida", row.Cells(5).Value)
                        com.Parameters.AddWithValue("@dra", row.Cells(7).Value)
                        com.ExecuteNonQuery()

                    ElseIf Me.DataGridView1.Rows(i).Cells("Type").Value = "Debit" Then
                        com.CommandText = "Update COA set opening_balance = opening_balance + @dr where AccountID=@id"
                        com.Parameters.AddWithValue("@id", row.Cells(5).Value)
                        com.Parameters.AddWithValue("@dr", row.Cells(7).Value)
                        com.ExecuteNonQuery()
                    End If

                Next
            End Using
        End Using
    Next
    Dim result1 As Integer = MessageBox.Show("Posted Successfully. Do you want to print", "Entry Posted Successfully", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
    If result1 = DialogResult.No Then
        DataGridView1.DataSource = Nothing
        DataGridView1.Rows.Clear()
        Call AddNewToolStripMenuItem_Click(sender, e)
    ElseIf result1 = DialogResult.Yes Then
        cpvreport()
        DataGridView1.DataSource = Nothing
        DataGridView1.Rows.Clear()
        Call AddNewToolStripMenuItem_Click(sender, e)
    End If

datagrid image

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...