1

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";
        }
    }
  • You will need to bind the data to combobox in a method, and call this method right after you did the insert. But I can't seem to find your databinding logic in the code you posted... – Laurent S. May 03 '13 at 13:51
  • I bound the combobox within the design of the form, not within the code. – RampantLion May 03 '13 at 14:04
  • The you should maybe do it from the code. Or at least I don't know how to re-bind it if not from within the codebehind – Laurent S. May 03 '13 at 14:05
  • I'm not sure how I would go about binding it through code, any pointers? – RampantLion May 03 '13 at 14:09
  • Well, there are many different approaches. I think this article would be a good start : http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial – Laurent S. May 03 '13 at 14:11

0 Answers0