Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 29th, 2011, 09:21 PM
Authorized User
 
Join Date: Mar 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Thumbs up Update multiple tables with EntityDataSource

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?

Reply With Quote
  #2 (permalink)  
Old March 30th, 2011, 02:39 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

I think this is caused by the fact you're wrapping the myPatient object (a bit confusing name, as it's really a context object and you also have another patient parameter) inside a Using statement in your AddPatIsGuarantor method. When the Using block ends, .NET calls Dispose on that object which means the object is destroyed when you exit the method.

Remove the Using block (since the ObjectContext is already disposed off by the main code) and it should work. Also, you can change the ByRef back to ByVal. ObjectContext is a reference type, so even when you pass it ByVal, you're still pointing to the same object. It's the pointer to the ObjectContext that gets ByVal'ed (copied) or ByRef'ed.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
alharlow (March 30th, 2011)
  #3 (permalink)  
Old March 30th, 2011, 11:44 PM
Authorized User
 
Join Date: Mar 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default Using

Thanks! Removing the 'using' statement fixed it. Didn't realize that 'dispose' was called when 'using' ended.

Reply With Quote
  #4 (permalink)  
Old March 31st, 2011, 03:15 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yes, that's pretty much the only thing it does. Using a statement like this (no pun intended):

Code:
 
Using (myWhatever As Whatever = new Whatever())
  ' Work with myWhatever
End Using
actually compiles to:

Code:
 
Dim myWhatever As Whatever
Try
  myWhatever = New Whatever()
  ' Work with myWhatever
Finally
  myWhatever.Dispose()
End Try
Since the code needs to be able to call Dispose, this only works for classes that implement the IDisposable interface (and therefore are guaranteed to have a Dispose method).

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error with EntityDataSource TeaDrinkingGeek BOOK: Beginning ASP.NET 4 : in C# and VB 3 March 20th, 2011 01:54 PM
Questions about EntityDataSource and dynamic binding Rushino ASP.NET 4 General Discussion 4 November 22nd, 2010 09:58 AM
Importing Multiple files in Multiple tables Versi Suomi Access 6 June 1st, 2005 08:47 AM
update to multiple tables elania PHP Databases 1 February 21st, 2005 03:10 PM
Update Multiple Related Tables with 1 Form stephaniejones SQL Server ASP 4 July 13th, 2003 08:52 PM



All times are GMT -4. The time now is 08:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.