Quantcast
Viewing all articles
Browse latest Browse all 27072

VS 2010 [RESOLVED] roll back insert statements

Code:

Try
                    'inserting the invoice number and its details
                    objcommand = New SqlCommand
                    objconnection = New SqlConnection
                    objconnection.ConnectionString = Variables.connstring
                    objcommand.Connection = objconnection
                    objcommand.CommandText = "Insert into InvDetails (InvoiceNo, ClientID, FileRef, Remarks, PORef) Values(@InvoiceNo, @ClientID, @FileRef, @Remarks, @PORef) "
                    objcommand.Parameters.AddWithValue("@InvoiceNo", txtInvoiceNo.Text)
                    objcommand.Parameters.AddWithValue("@ClientID", txtClientID.Text)
                    objcommand.Parameters.AddWithValue("@FileRef", txtFileRef.Text)
                    objcommand.Parameters.AddWithValue("@Remarks", txtRemarks.Text)
                    objcommand.Parameters.AddWithValue("@PORef", txtPORef.Text)
                    objconnection.Open()
                    invd = objcommand.ExecuteNonQuery()
                    objconnection.Close()
                    'for loop to go through each item and insert in db
                    For i = 0 To dgvVInvoiceEntries.Rows.Count - 1
                        objcommand = New SqlCommand
                        objconnection = New SqlConnection
                        objconnection.ConnectionString = Variables.connstring
                        objcommand.Connection = objconnection
                        objcommand.CommandText = "Insert into InvAmt (InvoiceNo, SerialNo, Description, Qty, UnitPrice, Amount) Values(@InvoiceNo, @SerialNo, @Description, @Qty, @UnitPrice, @Amount)"
                        objcommand.Parameters.AddWithValue("@InvoiceNo", txtInvoiceNo.Text)
                        objcommand.Parameters.AddWithValue("@SerialNo", dgvVInvoiceEntries.Rows(i).Cells(0).Value.ToString)
                        objcommand.Parameters.AddWithValue("@Description", dgvVInvoiceEntries.Rows(i).Cells(1).Value.ToString)
                        objcommand.Parameters.AddWithValue("@Qty", dgvVInvoiceEntries.Rows(i).Cells(2).Value.ToString)
                        objcommand.Parameters.AddWithValue("@UnitPrice", dgvVInvoiceEntries.Rows(i).Cells(3).Value.ToString)
                        objcommand.Parameters.AddWithValue("@Amount", dgvVInvoiceEntries.Rows(i).Cells(4).Value.ToString)
                        objconnection.Open()
                        inva = objcommand.ExecuteNonQuery()
                        objconnection.Close()
                    Next
                    If invd = 1 And inva = dgvVInvoiceEntries.Rows.Count Then
                        objcommand = New SqlCommand
                        objconnection = New SqlConnection
                        objconnection.ConnectionString = Variables.connstring
                        objcommand.Connection = objconnection
                        objcommand.CommandText = "Update Inv set status='Pending' where InvoiceNo='" & txtInvoiceNo.Text & "'"
                        objconnection.Open()
                        invd = objcommand.ExecuteNonQuery()
                        objconnection.Close()
                        MessageBox.Show("Success")
                        populateVinv()
                        ToolStripStatusLabel1.Text = txtInvoiceNo.Text & " saved."
                        btnNew.Enabled = True
                    Else
                        MsgBox("Error")
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try

hi guys,
I am developing a small application for generating invoices. When I have to save the invoice, first i have to save the invoice number and its details in a table and then i use a for loop to insert all items and its prices in a separate table.

lets say out of 3 items, the last item could not be inserted in the database. for me, i would like to roll back the changes I made.

how do i manage this in my application?

Rgds,

Ashley

Viewing all articles
Browse latest Browse all 27072

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>