Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Debugging shows listA.Items.Count returns 0;
See the debugger information here

attaching my codebehind what I have tried :

C#
public void Populate_ListBoxes(string subj)
{
  var elements = subj.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
  string lname = null;
  foreach (string items in elements)
  {
    lname = Get_Subjects(items);
    ListItem li = new ListItem();            
    li.Text = lname;
    li.Value = items;
    //ListItem la = listA.Items.FindByValue(items);
    listB.Items.Add(li);
    //compare lname to each of the listItem's text value
    //If comparison successful --> Remove that item from list.
    //I dont know how to compare because listA.Items.Count returns 0
    //How can it return 0 if sqlDataSource is populating listA with some listItems ? ? ??
  }
}

The function Get_Subjects gets the subject name corresponding to subjectID from a master table.
I debugged the code and found
C#
listA.Items.Count
returns "0"
I fail to understand where I'm going wrong. Any help is appreciated!
aspx:
ASP.NET
<td>
<asp:ListBox ID="listA" runat="server" DataSourceID="SqlDataSourceSubjects" 
  DataTextField="subject" DataValueField="subjectID" Rows="6" 
  SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSourceSubjects" runat="server" 
 ConnectionString="<%$ ConnectionStrings:myconnectionstring %>" 
  SelectCommand="SELECT subjectID, subject FROM mast_subject">
</asp:SqlDataSource>
</td>
<td>
<asp:Button ID="btnlistAtolistB" runat="server" 
OnClick="btnlistAtolistB_Click" Text=">>" />
 
<asp:Button ID="btnlistBtolistA" runat="server"                                        OnClick="btnlistBtolistA_Click" Text="<<" />
</td>
<td>
<asp:ListBox ID="listB" runat="server" Rows="6" SelectionMode="Multiple">
</asp:ListBox>
</td>
Posted
Updated 14-Oct-13 22:35pm
v6
Comments
Rahul Vohra 13-Oct-13 1:55am    
Please check this link for image showing the same : http://goo.gl/AamnJ9
JoCodes 13-Oct-13 4:49am    
Are you sure you are binding the ListA before you are performing an edit?
Rahul Vohra 14-Oct-13 3:59am    
I'm not sure if that is the problem. But I am using an sqldatasource and I guess sqldtasource binds the listA on page load! I was not sure about this also as I'm new to asp.net and I don't know the life cycle of the controls, so I tried calling databind() before my edit operation but it still did not work. Although I am now able to add those items to listB but still unable to remove them from listA.
Rahul Vohra 14-Oct-13 4:15am    
And yes, the values are proper. Also I'm sure the problem is about binding as you mentioned. But please tell me how can I bind on page load if I'm using sqlDataSource!
CodeBlack 14-Oct-13 9:36am    
can you update your question with your cs and aspx code ?

Move listA.Items.Clear(); from the for loop because your listA items will cleared after first item added to listB.
So, first add all items from listA to listB and then clear the listA. It will work.
 
Share this answer
 
Comments
Rahul Vohra 14-Oct-13 14:33pm    
@Phani First of all that listA.Items.Clear() is not the solution which I'm looking for I forgot to update it my bad. I just want to remove few elements from listA not all. Suppose I fetch some text from my database and stored them in some array say like [subject1, subject5, subject8]. Now listA is bound to sqlDataSource which automatically loads the listA with subjects from 1-10 (say). Now I know clear() would clear all from 1-10. I don't want that. I just want to remove only a few. So it should be done with Remove() or RemoveAt() but I have tried everything but unable to compare some text to the listItem's text. That is perfect solution to what I'm trying to achieve but I just don't how to do it!
ListBox.Items.Remove(index);

pass the index of item in the listbox
 
Share this answer
 
Comments
Rahul Vohra 15-Oct-13 8:58am    
@Ajay J Singh Remove() method doesn't accept index.. RemoveAt() method does and I have tried that as well. If you read my question again I've mentioned where the problem is I just don't know how to fix it. The problem is that listA.Items.Count returns 0. But it should not because I'm populating listA with sqlDataSource as you can see in my aspx. Don't know why is this happening!

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