请看下我这段代码,代码运行没问题,第一次显示的时候也没问题,第二次乃至以后显示就出现问题了。string sql = string.Format("select c.id,c.name from active a,tag b,employee c where a.tagid = b.tagid and b.id = c.id group by a.tagid having count(*)>1");
conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                if (!sdr.HasRows)
                {
                    MessageBox.Show("Error");
                }
                else
                {
                    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                    sda.Fill(ds, "employee");
                    DataTable datatable = ds.Tables["rfid_employee"];
                    int width = 110; int height = 250;
                    this.pictureBox1.Controls.Clear();
                    for (int i = 0; i < datatable.Rows.Count; i++)
                    {
                        Label lb2 = new Label();
                        lb2.Text = datatable.Rows[i]["id"].ToString() + datatable.Rows[i]["name"].ToString();
                        lb2.AutoSize = true;
                        lb2.BackColor = Color.Yellow;
                        lb2.Location = new System.Drawing.Point(width, height);
                        lb2.Refresh();
                        this.pictureBox1.Controls.Add(lb2);
                        width += 80;
                    }
                }
conn.Close();这段代码运行没出问题,第一次显示的时候出现例如:111张三  112李四 ,datatable.Rows.Count = 2; 第二次显示的时候出现111张三  112李四  111张三  112李四; 第三次的时候以此类推:111张三 112李四  111张三  112李四 111张三  112 李四.......  请问这是哪个地方出问题了

解决方案 »

  1.   

    绑定数据源之前要清空
    Clear();
      

  2.   

    有没有放到if(! Page.IsPostBack)
             {
                 ....
             }里面?
      

  3.   

       this.pictureBox1.Controls.Add(lb2);
    每次执行都会添加的
      

  4.   

    thank you !我确实没清空!
      

  5.   


    不是有this.pictureBox1.Controls.Clear();么...
      

  6.   

    DataTable datatable = ds.Tables["rfid_employee"];
    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
    ds.Tables["rfid_employee"].Clear();   //加上这句,顺序也变一下吧               
    sda.Fill(ds, "employee");