I'm building an app (while following along Imar Spaanjaars's "Beginning ASP.NET 4 in C# and
VB.NET"). I have two tables that I want to update as the result of a button click. I am using the same method as shown in step 6 on page 559 of the book. Before executing the .savechanges() line of code I call a second function to load an object for the second table. When I return to the first routine and execute the .savechanges() I get the message "
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. Here is the code:
Code:
Protected Sub bAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bAdd.Click
Using myPatient As New PrimusEntities1()
Dim patient As New patient_info()
patient.last_name = LastName.Text
patient.first_name = FirstName.Text
patient.middle_name = MidName.Text
patient.address1 = Addr1.Text
patient.address2 = Addr2.Text
patient.city = City.Text
patient.state = StateDropDownList.Text
patient.zip = Zip.Text
patient.DOB = Convert.ToDateTime(DOB.Text)
patient.spouse_name = SpouseName.Text
patient.home_phone = HomePhone.Text
patient.work_phone = WorkPhone.Text
patient.cell_phone = CellPhone.TextMode
patient.employer = employer.Text
patient.emergency_contact = emergency_contact.Text
patient.emergency_relationship = emergency_relationship.Text
patient.emergency_phone = emergency_phone.Text
patient.insur_id = InsurDropDownList.Text
patient.HICN = HICN.Text
patient.pwr_of_attorney = PwrOfAttorney.Text
If PatIsGuarantor.Checked Then
patient.patient_is_guarantor = 1
AddPatIsGuarantor(myPatient, patient)
Else
patient.patient_is_guarantor = 0
End If
patient.insert_user_id = "akh"
patient.inserttimestamp = Now
myPatient.AddTopatient_info(patient)
myPatient.SaveChanges() <----Error happens executing this
PatientAdded.Visible = True
Response.Redirect("patient_add.aspx")
End Using
End Sub
Protected Function AddPatIsGuarantor(ByRef myPatient As PrimusEntities1, ByVal patient As patient_info) As Boolean
AddPatIsGuarantor = True
Using myPatient
Dim myguarantor As New guarantor_info
myguarantor.last_name = patient.last_name
myguarantor.first_name = patient.first_name
myguarantor.middle_name = patient.middle_name
myguarantor.address1 = patient.address1
myguarantor.address2 = patient.address2
myguarantor.city = patient.city
myguarantor.state = patient.state
myguarantor.zip = patient.zip
myguarantor.DOB = patient.DOB
myguarantor.ssn = patient.ssn
myguarantor.home_phone = patient.home_phone
myguarantor.work_phone = patient.work_phone
myguarantor.cell_phone = patient.cell_phone
myguarantor.employer = patient.employer
myguarantor.insert_user_id = "akh"
myguarantor.inserttimestamp = Now
myPatient.AddToguarantor_info(myguarantor)
End Using
End Function
Anyone know what I'm doing wrong here?
