Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hullo Good Friends, :laugh:
Please help me. I need your help. ;)
I encounter error message when I trid to refill the Listbox after the user input the data into the parameter textbox. I tried to clear the display in the listbox and it generate this error message:

Items collection cannot be modified when the DataSource property is set.

This is the coding that generate it:
this.listBox1.Items.Clear();

Here are the overall coding:
private void FLoadListBox(string strTagName)
  {
             this.listBox1.Items.Clear();
  if (this.txtInput.text == "Product")
  {
        string strSql = "Select Product from TblSales";
  }

  else
      {
       string strSql = "Select Orders from TblSales";
  }

              sqlconn = new SqlConnection(connstr);
              sqlconn.Open();
              SqlDataAdapter DA = new SqlDataAdapter(strSql, sqlconn);
              System.Data.DataSet DS = new DataSet("DS");
              DS.Clear();
              DS.Tables.Clear();
              DA.Fill(DS, "Display");

  if (this.txtInput.text == "Product")
  {
           this.listBox1.DisplayMember ="Product";
       this.listBox1.DataSource = DS.Tables["Display"];
  }
  else
  {
       this.listBox1.DisplayMember ="Orders";
       this.listBox1.DataSource = DS.Tables["Display"];
  }
  sqlconn.close();
}


Please help me. How to clear the current display on Listbox before refilling it with new dataset
Posted
Comments
sandipapatel 5-Dec-10 23:32pm    
Try out : listBox1.DataSource = null; in second if-else both block. Also try out listBox1.DataBind() in last line.
Hope it will work

Try out : listBox1.DataSource = null; in second if-else both block. Also try out listBox1.DataBind() in last line.
Hope it will work
 
Share this answer
 
This is becuase your list box is bound to a Data Table, please remove the binding before deleting the items

Set the data source to null

this.listBox1.DataSource = null;
this.listBox1.Items.Clear();
 
Share this answer
 
Hullo Awesome sandipapatel, ksullas :laugh:
Thanks to both of you for sharing your knowledge with me.

This coding :listBox1.DataSource = null is the awesome coding and I have tried it out and it's working very well.


:rolleyes:
Both of you are wonderful and awesome and generous in sharing and helping me. I am so glad to meet both of you at this wonderful FORUM.
Appreciate that very much. :-D

Both of you have a Good Day.

Cheers,
Lennie
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900