是这样的 我有一个确定键(btnsure) 用户名写下去了 按确定 combox里的选项就跳出来了 我又多按了几下确定键 结果combox里多出了好多的选项而且是重复的  我的combox里的选项原本只有两个 怎么能让他不重复呢?
        private void btnSure_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select count(*) from protect where UserName='{0}'",this.tbName.Text);
            SqlCommand sqlcom = new SqlCommand(sql, DBhelper.connection);
            DBhelper.connection.Open();
            int count = int.Parse(sqlcom.ExecuteScalar().ToString());
            DBhelper.connection.Close();
            if (count > 0 )
            {                string sqlu = string.Format("select * from protect where UserName='{0}'", this.tbName.Text);
                SqlCommand sqlc = new SqlCommand(sqlu, DBhelper.connection);
                DBhelper.connection.Open();
                SqlDataReader reader = sqlc.ExecuteReader();
                if (reader.Read())
                {
                    
                    cmbQuestion.Items.Add(reader["Question1"]);
                    cmbQuestion.Items.Add(reader["Question2"]);
                
                }
                DBhelper.connection.Close();
            }
            else
            {
                MessageBox.Show("该用户不存在!");
            }

解决方案 »

  1.   

    每次点击的时候清空combox中的内容
      

  2.   

    private void btnSure_Click(object sender, EventArgs e)
      {
      string sql = string.Format("select count(*) from protect where UserName='{0}'",this.tbName.Text);
      SqlCommand sqlcom = new SqlCommand(sql, DBhelper.connection);
      DBhelper.connection.Open();
      int count = int.Parse(sqlcom.ExecuteScalar().ToString());
      DBhelper.connection.Close();
      if (count > 0 )
      {  string sqlu = string.Format("select * from protect where UserName='{0}'", this.tbName.Text);
      SqlCommand sqlc = new SqlCommand(sqlu, DBhelper.connection);
      DBhelper.connection.Open();
      SqlDataReader reader = sqlc.ExecuteReader();
      if (reader.Read())
      {
       cmbQuestion.Items.Clear(); 
      cmbQuestion.Items.Add(reader["Question1"]);
      cmbQuestion.Items.Add(reader["Question2"]);
        
      }
      DBhelper.connection.Close();
      }
      else
      {
      MessageBox.Show("该用户不存在!");
      }
      

  3.   

    要这样 
    private void btnSure_Click(object sender, EventArgs e)
      {
    if(cmbQuestion.Items.Count>0)
    {
        cmbQuestion.Items.Clear();
    }
      string sql = string.Format("select count(*) from protect where UserName='{0}'",this.tbName.Text);
      SqlCommand sqlcom = new SqlCommand(sql, DBhelper.connection);
      DBhelper.connection.Open();
      int count = int.Parse(sqlcom.ExecuteScalar().ToString());
      DBhelper.connection.Close();
      if (count > 0 )
      {  string sqlu = string.Format("select * from protect where UserName='{0}'", this.tbName.Text);
      SqlCommand sqlc = new SqlCommand(sqlu, DBhelper.connection);
      DBhelper.connection.Open();
      SqlDataReader reader = sqlc.ExecuteReader();
      if (reader.Read())
      {  cmbQuestion.Items.Add(reader["Question1"]);
      cmbQuestion.Items.Add(reader["Question2"]);
       
      }
      DBhelper.connection.Close();
      }
      else
      {
      MessageBox.Show("该用户不存在!");
      }
      

  4.   

     if (reader.Read())
      {
    cmbQuestion.Items.Clear(); 
      cmbQuestion.Items.Add(reader["Question1"]);
      cmbQuestion.Items.Add(reader["Question2"]);
        
      }
      

  5.   

    呵呵  大家说的都可以 但是怎么设置combox选项默认为Question1呢
      

  6.   

    你说的这个方法可以啊  再问一下怎么设置combox选项默认为Question1呢
      

  7.   

    ComboBox.Items.Contains(object value)
    combobox.Text="指定的value";