C# opens a Dataset but then never updates until I close and re-open C# Express
<p>I'm just getting into this C# dealing with tables and Dataset, i'm using the Visual Studio 2010 Express edition to create the code.</p>

<p>I have created a form, it's unbound because I wanted to populate a list of <code>LastNames</code> in a combo box that were actual unique, on selection the FirstNames combo box populates with the matching <code>FirstName</code>'s for the <code>LastName</code> selected. </p>

<p>Once selected I populate the rest of the fields on the form.</p>

<p>This all works as I wanted.</p>

<p>My issue is this, when I open the table "<code>tbl_VisitorLog</code>" and add another line and re-select to update the screen the new entry is not displayed (i'm using the "EndOnSite" field to validate the updated screen into the <code>txt_LastSeen</code> text box). Even if I close the form, save the form and execute the program again the new data is not populated into the DataSet. </p>

<p>I think its because I'm not opening and closing the connection, actually I'm not clear what is opening the connection in the code :( - This said if I close the connection and refresh in the database explorer this does not work either. In fact, the only way to get access to the new data is to close C# Express and reload the project!!</p>

<p>What I'm I not doing?</p>

<p>Sorry if some of this code is a bit weird, but it works so far :)  - Please make comments on anything I could do better.</p>

<pre><code>    public partial class frm_SelectVisitor : Form
{
    public frm_SelectVisitor()
    {
        InitializeComponent();
    }

    private void frm_SelectVisitor_Load(object sender, EventArgs e)
    {
        // As we are not going to make the form bound to the database itself
        // i seem need to set the connections to the datasets.
        // The easy way to do this is to bind the form to a field from each table one by one.
        // Note, the form can only be bound to one, but it does not matter, we just want the
        // reference here and then we will clear the last bound to "none".
        // Form Properties --&gt; DataBindings --&gt; Advanced...
        // Binging --&gt; Other Data Sources --&gt; Project Data Sources --&gt; 
        // Then click on each table, then a key field  --&gt; OK
        // At this stage the code for that table will be place in the "Form Load" event.
        // Do all the other tables you need to access.
        // Once complete you can set the bindings to "None".
        // Note, it seems you MUST load tables in the base order, i.e. for a one to many relationship
        // load the "many" table first! Otherwise the load of the "one" will just hang!!
        // Use Breakpoints to see how far you get through these !!!

        // This line of code loads data into the 'db_SiteVisitorsLogDataSet.tbl_VisitorTypes' table.
        this.tbl_VisitorTypesTableAdapter.Fill(this.db_SiteVisitorsLogDataSet.tbl_VisitorTypes);
        // This line of code loads data into the 'db_SiteVisitorsLogDataSet.tbl_VisitorDetails' table.
        this.tbl_VisitorDetailsTableAdapter.Fill(this.db_SiteVisitorsLogDataSet.tbl_VisitorDetails);
        // This line of code loads data into the 'db_SiteVisitorsLogDataSet.tbl_VisitorLog' table. 
        this.tbl_VisitorLogTableAdapter.Fill(this.db_SiteVisitorsLogDataSet.tbl_VisitorLog);

        resetForm();
        populateLastNames();            
    }

    private void resetForm()
    {
        // Basic activatation of all on form controls followed by clearing any text boxes.
        // Combo boxes will also be cleared of any items
        cbo_LastName.Visible = true; cbo_LastName.Enabled = true; cbo_LastName.Text = ""; cbo_LastName.Items.Clear();
        cbo_FirstName.Visible = true; cbo_FirstName.Enabled = false; cbo_FirstName.Text = ""; cbo_FirstName.Items.Clear();
        txt_VisitorID.Visible = true; txt_VisitorID.Enabled = false; txt_VisitorID.Text = "";
        txt_DateCreated.Visible = true; txt_DateCreated.Enabled = false; txt_DateCreated.Text = "";
        txt_VisitorType.Visible = true; txt_VisitorType.Enabled = false; txt_VisitorType.Text = "";
        txt_LastSeen.Visible = true; txt_LastSeen.Enabled = false; txt_LastSeen.Text = "";
        //this. = "Site Visitor Control System";
    }

    private void cbo_LastName_SelectedIndexChanged(object sender, EventArgs e)
    {
        populateFirstNames(cbo_LastName.Text);
    }

    private void cbo_FirstName_SelectedIndexChanged(object sender, EventArgs e)
    {
        populateVisitorDetails(cbo_LastName.Text, cbo_FirstName.Text);
    }


    private void populateLastNames()
    {
        // First we need to clear any previous contents.
        cbo_LastName.Visible = true; cbo_LastName.Enabled = true; cbo_LastName.Text = ""; cbo_LastName.Items.Clear();

        // We only want unique Last Names, no point having 10 Smiths.
        var lastNames = from n in db_SiteVisitorsLogDataSet.tbl_VisitorDetails
                        group n by n.LastName into n
                        select new { uniqueName = n.Key };

        // Now populate the combo box with the results.
        foreach (var uniqueName in lastNames)
            cbo_LastName.Items.Add(uniqueName.uniqueName);

        // And end by dropping down the combo box to direct the user what you want them to do.
        cbo_LastName.Text = "Select a Name...";
        cbo_LastName.DroppedDown = true;
    }

    private void populateFirstNames(string par_LastName)
    {
        // First we need to clear any previous contents, and activate the combo box.
        cbo_FirstName.Visible = true; cbo_FirstName.Enabled = true; cbo_FirstName.Text = ""; cbo_FirstName.Items.Clear();

        // We only want the First Names that belong to the Last Name selection.
        int counter = 0;
        var firstNames = from n in db_SiteVisitorsLogDataSet.tbl_VisitorDetails
                         where n.LastName == par_LastName
                         group n by n.FirstName
                         into n
                         select new { uniqueName = n.Key };
        foreach (var n in firstNames)
        {
            cbo_FirstName.Items.Add(n.uniqueName);
            counter++;
        }
        // The counter should never be Zero as the LastName is set by a Combo box.
        // However, Just in case someone calls the Method incorrectly
        if (counter == 0)
        {
            throw new Exception("Last Name is incorrect on Select Visitor Form");

        }
        // And end by dropping down the combo box to direct the user what you want them to do next.
        cbo_FirstName.Text = "Select a Name...";
        cbo_FirstName.DroppedDown = true;
    }

    private void populateVisitorDetails(string par_LastName, string par_FirstName)
    {
        // First we need to clear any previous contents, and activate the fields. 
        txt_VisitorID.Visible = true; txt_VisitorID.Enabled = true; txt_VisitorID.Text = "";
        txt_DateCreated.Visible = true; txt_DateCreated.Enabled = true; txt_DateCreated.Text = "";
        txt_VisitorType.Visible = true; txt_VisitorType.Enabled = true; txt_VisitorType.Text = "";
        txt_LastSeen.Visible = true; txt_LastSeen.Enabled = true; txt_LastSeen.Text = "";

        // Lets get the VisitorID, First Created On Date and their normal reason for visiting.
        var visitorDetails = from vd in db_SiteVisitorsLogDataSet.tbl_VisitorDetails
                             join vt in db_SiteVisitorsLogDataSet.tbl_VisitorTypes
                             on vd.VisitorType equals vt.VisitorTypeID
                             into myVT
                             where vd.LastName == par_LastName
                             where vd.FirstName == par_FirstName
                             select new { vd, vd.tbl_VisitorTypesRow };

        // While there should only be one record I dont know how to pull them back without the foreach
        foreach (var vd in visitorDetails)
        {
            txt_VisitorID.Text = vd.vd.VisitorDetailsID.ToString();
            txt_DateCreated.Text = vd.vd.DateCreatedOn.ToShortDateString();
            txt_VisitorType.Text = vd.tbl_VisitorTypesRow.VisitorTypeText.ToString();
            txt_Company.Text = vd.vd.VisitorCompanyName.ToString();

            var visitorSeen = from vl in db_SiteVisitorsLogDataSet.tbl_VisitorLog
                              join vd2 in db_SiteVisitorsLogDataSet.tbl_VisitorDetails
                              on vl.VisitorDetailsID equals vd2.VisitorDetailsID
                              into myVD
                              where vl.VisitorDetailsID == vd.vd.VisitorDetailsID
                              orderby vl.EndOnSite
                              select new { vl };
            foreach (var vs in visitorSeen)
            {
                txt_LastSeen.Text = vs.vl.EndOnSite.ToShortDateString();
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // TODO: Make this refresh work!!
        // Does Not Work  //this.InitializeComponent();
        // This line of code loads data into the 'db_SiteVisitorsLogDataSet.tbl_VisitorTypes' table.
        //this.tbl_VisitorTypesTableAdapter.Fill(this.db_SiteVisitorsLogDataSet.tbl_VisitorTypes);
        //// This line of code loads data into the 'db_SiteVisitorsLogDataSet.tbl_VisitorDetails' table.
        //this.tbl_VisitorDetailsTableAdapter.Fill(this.db_SiteVisitorsLogDataSet.tbl_VisitorDetails);
        //// This line of code loads data into the 'db_SiteVisitorsLogDataSet.tbl_VisitorLog' table. 
        //this.tbl_VisitorLogTableAdapter.Fill(this.db_SiteVisitorsLogDataSet.tbl_VisitorLog);
        //db_SiteVisitorsLogDataSet.AcceptChanges();
        //db_SiteVisitorsLogDataSet.GetChanges();

    }
}
</code></pre>
