Datacontext and classes

Posted on 2009-12-23
Last Modified: 2013-11-11
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
Question by:scottsanpedro
    LVL 83

    Expert Comment

    I tend to create a datacontext on each form where i use the linq classes. A global datacontext would be a disaster.
    LVL 1

    Author Comment

    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

    LVL 83

    Accepted Solution

    I keep the context open in the form at all times. Your approach is fine as well.
    LVL 1

    Author Closing Comment

    I believe I was looking for confirmation that my approach was correct and feel I received that.
    Many thanks for your time

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now