0

I've checked the answers on this topic however I still have no idea why this is not working! PLEASE HELP!

    private void btnAdd_Click(object sender, EventArgs e)
    {
        SqlCeCommand insTitle = new SqlCeCommand("Insert into Titles(Title) values('" + txtAddTitle.Text +"')");
        insTitle.Connection = dbConnection;

        try 
        {
            if (dbConnection.State == ConnectionState.Closed) { dbConnection.Open(); }
            insTitle.ExecuteNonQuery();


            this.hRDataSet.AcceptChanges();
            this.titlesTableAdapter.Update(this.hRDataSet);
            this.tableAdapterManager.UpdateAll(this.hRDataSet);

            lstTitles.BeginUpdate();
            lstTitles.DataSource = titlesBindingSource;
            lstTitles.DisplayMember = "Title";
            lstTitles.ValueMember = "Title_ID";
            lstTitles.EndUpdate();
        }
        catch (Exception insErr)
        {
            MessageBox.Show(insErr.Message);
        }
    }

The listbox "lstTitles" won't refresh and doesn't show the added items despite the fact that they are in the database!

user799920
  • 19
  • 1
  • 5
  • Do you really understand the code you wrote? This looks suspiciously like [cargo cult programming](http://en.wikipedia.org/wiki/Cargo_cult_programming)... – Thomas Levesque Mar 21 '12 at 21:45
  • I've tried so many different ways of coding ... it is all gibraish to me now. Can you help after the "executenonquery" ? – user799920 Mar 21 '12 at 21:48

1 Answers1

0

The Update method of the DataAdapter is used to update the database with the changes made in the DataSet. What you need to do here is the opposite: you need to update the DataSet with the modified data from the database, so you should use Fill, not Update.

Anyway, your approach is not optimal; since you're working with datasets, you should add the new value to the appropriate table in the DataSet, and then update the database with the Update method. The ListBox will automatically pick up the changes.

private void btnAdd_Click(object sender, EventArgs e)
{
    try 
    {
        // Add a row to the DataTable
        DataRow row = hRDataSet.Titles.NewRow();
        row["Title"] = txtAddTitle.Text;
        hRDataSet.Titles.Rows.Add(row);

        // Update the database
        this.titlesTableAdapter.Update(this.hRDataSet);

        // That's it, you're done ;)
    }
    catch (Exception insErr)
    {
        MessageBox.Show(insErr.Message);
    }
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Thanks for explaining that... the only reason I went down this road is because my .sdf is not being updated with new records if I update the dataset first... at least not when I click on "show table data" under server explorer! – user799920 Mar 21 '12 at 22:12
  • and that is because I was running it in debug mode! DUHHH! – user799920 Mar 21 '12 at 22:38
  • @user799920, just change the "Copy to output directory" property of your sdf file – Thomas Levesque Mar 21 '12 at 22:43