1

在我的表单中,我使用组合框从数据库中选择一条记录,然后填充文本框。每当我添加记录(或更新、删除)时,组合框在我关闭并重新运行程序之前都不会获得更新的数据。组合框通过设计视图进行数据绑定。基本上,当我更改表中的数据时,我需要这个组合框实时更新。感谢任何帮助,谢谢。

C# - 视觉工作室 2010

代码:

//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";
        }
    }
4

0 回答 0