Datacontext and classes

Hi, I have been banging my head on this issue for a bit of time now.
I have a windows form application and I'm attempting my first discovery of linq to sql. Using sql server 2005.
To simplify I have a main form with fields. Address1 address2 etc
I have a class names clsCompany with has the logic to run a query and retrieve records via linqtosql. I then pass the table back to the main form via a property and then bind it to a bindingsource. No great shakes yet.
I then update some fields and want to run db.submitchanges.
Now the datacontext cannot be set as global as the data will go stale.
So the question I really need answering.
What is the recommended way for me to set this model up.
Draw the data via the first datacontext. Let it go out of scope and then on update create a new one and pass the fields independently?
Actually keep the datacontext global/live?
The first issue obviously losses the change tracking ability etc
Thanks in advanced Scott

Also refer to
Who is Participating?
I keep the context open in the form at all times. Your approach is fine as well.
I tend to create a datacontext on each form where i use the linq classes. A global datacontext would be a disaster.
scottsanpedroAuthor Commented:
thanks for answering.
So do you keep the datacontext in scope all the time within the form?
I have since created a management class that has only a shared datacontext.
When searching a new record I use this each time. When saving a record I use the 'USING' syntax around the db.submitchanges() to kill the context off, then re-search on the same record to bring it back live.
Not sure if this is overkill to keep the datacontext fresh?
Public Class clsCompanyContext
    Public Shared mCompanyContext As CompanyControlDataContext

    Public Sub New()
        mCompanyContext = New CompanyControlDataContext
    End Sub

End Class

Open in new window

scottsanpedroAuthor Commented:
I believe I was looking for confirmation that my approach was correct and feel I received that.
Many thanks for your time
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.