Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 818
  • Last Modified:

Datacontext and classes

Hi, I have been banging my head on this issue for a bit of time now.
I have a vb.net 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?
OR
Actually keep the datacontext global/live?
OR
????
The first issue obviously losses the change tracking ability etc
REALLY CONFUSED
Thanks in advanced Scott

Also refer to
http://www.experts-exchange.com/Programming/Languages/.NET/LINQ/Q_23282867.html?sfQueryTermInfo=1+10+datacontext
0
scottsanpedro
Asked:
scottsanpedro
  • 2
  • 2
1 Solution
 
CodeCruiserCommented:
I tend to create a datacontext on each form where i use the linq classes. A global datacontext would be a disaster.
0
 
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?
Scott
Public Class clsCompanyContext
    Public Shared mCompanyContext As CompanyControlDataContext

    Public Sub New()
        mCompanyContext = New CompanyControlDataContext
    End Sub

End Class

Open in new window

0
 
CodeCruiserCommented:
I keep the context open in the form at all times. Your approach is fine as well.
0
 
scottsanpedroAuthor Commented:
I believe I was looking for confirmation that my approach was correct and feel I received that.
Many thanks for your time
Scott
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now