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.