Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Use the same transaction in several forms in VB.NEt?
Closed Thread
Old 06-08-2004, 07:21 PM   #1 (permalink)
 
Banned

Join Date: Jan 2004

Posts: 471

subdivizion

Default Use the same transaction in several forms in VB.NEt?

i have this database application i'm working on and i want to start a transaction in the first form from which data is entered and commit or rollback the same transaction in the last form. when i call the transaction object which was defined in the first form i can't access it in the last one. how do i get around this?
subdivizion is offline  
Old 06-08-2004, 07:25 PM   #2 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

Could we see your code?
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 06-08-2004, 08:01 PM   #3 (permalink)
 
Banned

Join Date: Jan 2004

Posts: 471

subdivizion

Default

well its very long code but here's a summary with the necessary parts. in the first form i have:

Dim insertData As String = strBuilder.ToString()

Dim cmdInsert As SqlCommand = New SqlCommand(insertData, SqlConnection1)

SqlConnection1.Open()

cmdInsert.ExecuteNonQuery()

SqlConnection1.Close()

Me.Close()
Dim frmEducation As New education
frmEducation.Show()

Catch ex As System.Data.SqlClient.SqlException
Dim j As Integer
For j = 0 To ex.Errors.Count - 1
MessageBox.Show("Error # " & j & ControlChars.Cr & _
"Error: " & ex.Errors(j).ToString() & ControlChars.Cr)
Next

SqlConnection1.Close()

End Try



and i'd like to just do:

Dim insertData As String = strBuilder.ToString()

Dim cmdInsert As SqlCommand = New SqlCommand(insertData, SqlConnection1)

SqlConnection1.Open()

dim insTrans = SqlConnection1.BeginTransaction("Transaction")

cmdInsert.Transaction=insTrans

cmdInsert.ExecuteNonQuery()

Me.Close()
Dim frmEducation As New education
frmEducation.Show()

Catch ex As System.Data.SqlClient.SqlException
Dim j As Integer
For j = 0 To ex.Errors.Count - 1
MessageBox.Show("Error # " & j & ControlChars.Cr & _
"Error: " & ex.Errors(j).ToString() & ControlChars.Cr)
Next

SqlConnection1.Close()

End Try


and in the last form i want to have:

Apply button event:
Dim insertData As String = strBuilder.ToString()

Dim cmdInsert As SqlCommand = New SqlCommand(insertData, SqlConnection1)

cmdInsert.ExecuteNonQuery()

insTrans.Commit()

SqlConnection1.Close()

Me.Close()

MessageBox.Show("Data was entered succesfully and an appointment has been made for 30 days from today. Check MS Outlook for details.")

Catch ex As System.Data.SqlClient.SqlException
Dim j As Integer
For j = 0 To ex.Errors.Count - 1
MessageBox.Show("Error # " & j & ControlChars.Cr & _
"Error: " & ex.Errors(j).ToString() & ControlChars.Cr)
Next

SqlConnection1.Close()


and in the Cancel button event:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try

insTrans.Rollback()

SqlConnection1.Close()

Catch ex As System.Data.SqlClient.SqlException
Dim j As Integer

For j = 0 To ex.Errors.Count - 1
MessageBox.Show("Error # " & j & ControlChars.Cr & _
"Error: " & ex.Errors(j).ToString() & ControlChars.Cr)

Next

SqlConnection1.Close()

End Try



but the problem i'm having is:
1.the transaction ends after the first form closes itself and
2.the transaction variable insTrans is obviously private in the first form and cannot be accessed by the last form.

I cannot make the variable public and then inherit the first form in the last form because all buttons and textboxes are also inherited and mess with the design.

sorry if this is tedious.
subdivizion is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On