Combobox not clearing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aparnakrish
    New Member
    • Sep 2010
    • 2

    Combobox not clearing

    Dear friends,
    I have 2 combo boxes in my project. When Iam selecting an item in first combo, its corresponding datawill display in the second one. But when am selecting next item, the previous items in the second combobox is not getting cleared. Combobox2.items .clear() doesn’t work in my prjt. Am in trouble. Can anyone pls help me out for the same.
    Here’s my code. I think the data I have given is sufficient.

    Code:
    private void cmbFileNo_SelectedIndexChanged(object sender, EventArgs e)
            {
             
                con.Open();
                cmd.Connection = con;
                fid = cmbFileNo.SelectedItem.ToString();
                cmbQuotationNo.Items.Clear(); 
                cmd.CommandText = "select * from FileTrack where FileNo='" + fid + "'";
                ad.SelectCommand = cmd;
                ad.Fill(dt);
                cmbQuotationNo.Items.Clear();             
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                   
                    if (dt.Rows.Count > 0)
                    {
                          
                          txtFileNme.Text = dt.Rows[i][1].ToString();
                          cmbQuotationNo.Items.Add(dt.Rows[i][2].ToString());
                          cmbQuotationDate.Items.Add(dt.Rows[i][3].ToString());
                          cmbSupplyOrderNo.Items.Add(dt.Rows[i][4].ToString());
                          cmbSupplyOrderDate.Items.Add(dt.Rows[i][5].ToString());
                          cmbInvoiceNo.Items.Add(dt.Rows[i][6].ToString());
                          cmbInvoiceDate.Items.Add(dt.Rows[i][7].ToString());
                        
                                        
                    }
                   
                }
               
                con.Close();
    Last edited by Markus; Sep 16 '10, 08:32 AM. Reason: Added [code] tags
  • Anton Zinchenko
    New Member
    • Sep 2010
    • 16

    #2
    I think the problem line in your code is:
    Code:
    ad.Fill(dt);
    You are using Fill method on the same DataTable multiply times, so the incoming rows are appended to the DataTable.

    Comment

    • Aparnakrish
      New Member
      • Sep 2010
      • 2

      #3
      C#

      Thanks for your reply. But how to rectify that? Am in starting stage of C#

      Comment

      • talhakarkun
        New Member
        • Jun 2010
        • 12

        #4
        i think problem is in ur datatable.
        when u fill data in datatable then previous data are also same in this table.and new data insert after that.
        So before u use same datatable
        first u want to set new object of datatable.
        like
        dt=new datatable();

        and after that fill Data in ur DataTable.

        thanks
        Talha Karkun

        Comment

        • MadhuriLatha
          New Member
          • Sep 2010
          • 3

          #5
          its better to show error msg. and in ur code there is no declaration nd defination fo datatable variable "dt"

          Comment

          Working...