in my form I'm using a combo box to be able to select a record from the database, and then populate text boxes. Whenever I add a record (or update, delete) the combobox doesn't get the updated data until I close and rerun the program. The combobox is databound via the design view. Basically I require this combobox to update in real-time when I change the data in the table. Appreciate any help, thanks.
C# - Visual Studio 2010
Code:
//Form load
private void Technician_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'technicianDataset.Technician' table. You can move, or remove it, as needed.
this.technicianTableAdapter.Fill(this.technicianDataset.Technician);
}
private void cmo_User_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\\temp\\Project\\WindowsFormsApplication2\\database.sdf"))
{
try
{
connection.Open();
string sqlText = "SELECT User_ID, Name, Password FROM Technician WHERE User_ID = @User_ID;";
SqlCeCommand command = new SqlCeCommand(sqlText, connection);
command.Parameters.AddWithValue("@User_ID", Convert.ToInt32(cmo_User.SelectedValue));
SqlCeDataReader reader = command.ExecuteReader();
while (reader.Read())
{
txt_userID.Text = reader["User_ID"].ToString();
txt_name.Text = reader["Name"].ToString();
txt_password.Text = reader["Password"].ToString();
}
reader.Close();
connection.Close();
} catch(SqlCeException exp){
Console.Write(exp.ToString());
}
}
}
//Add Record
using (SqlCeConnection connection = new SqlCeConnection(@"DataSource=C:\\temp\\Project\\WindowsFormsApplication2\\OrkneyCheese.sdf"))
{
connection.Open();
string sqlText = "INSERT INTO Technician VALUES (@userid, @name, @password);";
SqlCeCommand command = new SqlCeCommand(sqlText, connection);
command.Parameters.AddWithValue("@name", txt_name.Text);
command.Parameters.AddWithValue("@password", txt_password.Text);
command.Parameters.AddWithValue("@userid", Convert.ToInt32(txt_userID.Text));
command.ExecuteNonQuery();
connection.Close();
cmo_User.Update();
lbl_feedbackTech.Text = "User Successfully added";
}
}