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!
The
Updatemethod of theDataAdapteris used to update the database with the changes made in theDataSet. What you need to do here is the opposite: you need to update theDataSetwith the modified data from the database, so you should useFill, notUpdate.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 theUpdatemethod. The ListBox will automatically pick up the changes.