We help IT Professionals succeed at work.
Get Started

Linq2Sql Refresh DataContext

Last Modified: 2012-08-14
Hi experts!

So I am writing a relatively simple WinForms application for our companies inventory purposes. The database has three tables (Product, Team, and ErrorLog). Team has a one to many relationship with Product. I decided to use Linq2Sql as my ORM. I am used to using NHibernate in ASP.Net so it is quite different.

I have a Main form where a user can view available Teams, or select to create a new team.

Display available Teams in MainForm
        private void FrmProductInventoryMain_Load(object sender, EventArgs e)
                using (dbContext = new ProductInventoryDataContext())
                    this.contextBindingSource = new BindingSource { DataSource = dbContext.Teams.OrderBy(x => x.Name) };

                    cboAllTeams.ValueMember = "Id";
                    cboAllTeams.DisplayMember = "Name";
                    cboAllTeams.DataSource = contextBindingSource;

                    lblMemberOneData.DataBindings.Add("Text", contextBindingSource, "MemberOne");
                    lblMemberTwoData.DataBindings.Add("Text", contextBindingSource, "MemberTwo");
            catch (Exception)
                MessageBox.Show("Sorry, an unexpected error occured while trying to connect to the database",
                                "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

Open in new window

I am able to create the new team and it is added to the database with no issues.

Create New Team in Create Team Form
        private void btnSubmit_Click(object sender, EventArgs e)
            if (!ValidateMemberOne() || !ValidateMemberTwo()) return;

            // form is valid
            using (dbContext = new ProductInventoryDataContext())
                dbContext.Teams.InsertOnSubmit(new Team
                        Name = lblTeamNumber.Text,
                        MemberOne = txtNewMemberOne.Text.Trim(),
                        MemberTwo = txtNewMemberTwo.Text.Trim()



Open in new window

However, when the "Create Team" form closes, your are left with the "Main Form" which displays the available teams.

I am not sure how to go about updating the Main Form after a new Team has been created, save a last ditch option where I close the main form and reopen it...

I have been reading up as much as possible on how to manage my DataContext, and there seems to be a lot of discussion out there about the best way, or best practice.

Any suggestions ??
Watch Question
Most Valuable Expert 2011
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE