Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2009
    Posts
    168

    Unanswered: Transaction in vb.net

    Hi

    I have a problem with implementing transactions with my vb.net application. My code goes like this:

    public Function processCoursePayment()
    ---
    --- code
    ---

    mancosa.payCourse()

    ---
    --- code
    ---
    End Function

    Code:
    public Function payCourse()
    Dim sd As DataSet1TableAdapters.StudentPaymentTableAdapter = New DataSet1TableAdapters.StudentPaymentTableAdapter
            Dim dsPayCourse As DataSet1.StudentPaymentDataTable = New DataSet1.StudentPaymentDataTable
            Dim connection = sd.Connection
            Dim transaction As SqlTransaction
            Dim studentRow As DataRow = dsPayCourse.NewRow
            studentRow.Item("amount") = amount
            studentRow.Item("date") = payDate
            studentRow.Item("studentID") = studentID
            studentRow.Item("courseFeesID") = courseFeesID
            studentRow.Item("paymentMethodID") = paymentMethodID
            dsPayCourse.AddStudentPaymentRow(studentRow)
            Dim count As Integer = dsPayCourse.Count
            connection.Open()
            transaction = connection.BeginTransaction(IsolationLevel.Serializable)
            sd.Adapter.UpdateCommand.Transaction = transaction
            sd.Adapter.InsertCommand.Transaction = transaction
            Try
                sd.Adapter.Update(dsPayCourse)
                dsPayCourse.AcceptChanges()
                transaction.Commit()
            Catch ex As Exception
                transaction.Rollback()
                Console.WriteLine(ex.Message)
            Finally
                connection.Close()
            End Try
    End Function
    I had the exception that 'The Transaction property of the command has not been initialized.' But this is solved once I inserted the
    'sd.Adapter.InsertCommand.Transaction = transaction' part.

    However the problem is that when I run in debugger mode and I stop the running of the program before the commit() part, my data table still gets updated and the results are in the database, hence it has not performed the rollback properly. Also, like shown above when the payCourse() function exits, there are still code to be run in the main processCoursePayment() function. So I would want that if a rollback occured in the inner function, then the whole processCoursePayment() is aborted.

    Anyone can advise on this please? Thanks.
    Last edited by kpeeroo; 10-25-10 at 20:03.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •