Reputation:
I can't seem to get my ComboBox to refresh after I add new data to the Access database.
Here's the code I use to add the new data:
private void btnAddUser_Click(object sender, EventArgs e)
{
AccountForm actFrm = new AccountForm();
if (actFrm.ShowDialog() == DialogResult.OK)
{
try
{
this.oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO userTable (AccountName, Username, PopServer, PopPort, Psswrd, SmtpServer, SmtpPort, Email)" +
"VALUES ('" + actFrm.txtAccName.Text + "','" + actFrm.txtUsername.Text + "','" + actFrm.txtPop3.Text + "','" + actFrm.txtPop3Port.Text + "','" + actFrm.txtPassword.Text + "','" + actFrm.txtSmtp.Text + "','" + actFrm.txtSmtpPort.Text + "','" + actFrm.txtEmail.Text + "')";
//open the bridge between the application and the datasource
this.oleDbConnection1.Open();
this.oleDbDataAdapter1.InsertCommand.Connection = oleDbConnection1;
//execute the query
this.oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
//close the connection
this.oleDbConnection1.Close();
MessageBox.Show("User Added Successfully"); //inform the user
//tried here to refresh and even open close the myConn connection. to no avail
}
catch (System.Data.OleDb.OleDbException exp)
{
//close the connection
this.oleDbConnection1.Close();
MessageBox.Show(exp.ToString());
}
}
}
Upvotes: 0
Views: 5331
Reputation: 31
I recently had the same problem in VB.NET. I found that setting the datasoure to Nothing (null in C# I believe) and then assigning it to my list of objects solved the problem.
i.e.
attendanceDataFiles.DataSource = Nothing
attendanceDataFiles.DataSource = myFileList.files
Upvotes: 0
Reputation: 50225
Is your BindingSource using a DataSet? If so, you need to do the insert through the DataSet and, maybe, refresh your binding source. If you do it this way, you'll avoid the duplicated insert logic as well.
Alternatively, you can just refresh the DataSet but this method doesn't take advantage of the DataSet's powers and will lead to a lot duplicated code.
Upvotes: 2