Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i ve one list box that items not having line by line having like xxxx,yyyy,zzzzz.....how can i get that listbox text can any body know help me
Posted
Comments
Ankur\m/ 22-Jan-11 0:07am    
Do you mean your listbox has only one item and that is xxxx,yyyy,zzzzz...
And you want to get individual values like xxx and yyyy e.t.c

You question is not clear. You need to clarify.
Rupa1 22-Jan-11 0:11am    
i am adding items from gridview if itis checked then item will add the listbox after again one items add it will be add like , so all only one text how can i get that text...........
listbox1.text i wrote but its showing null
Ankur\m/ 22-Jan-11 0:33am    
Firstly, when you hover over my comment, you will see a reply link. Use that to reply otherwise I do not get notification for you comment.
Your question is still not clear. "listbox1.text i wrote but its showing null" means the items are not getting added to the listbox. Debug the code line by line and keep a watch on the required values. If you still can't solve it, paste the relevant code here.
Rupa1 22-Jan-11 0:39am    
string x = ds.Tables[0].Rows[0][0].ToString();
string get_value = ListBox1.Text.ToString();
con.Open();
DateTime dt = Convert.ToDateTime(DateTime.Now.ToString());
SqlCommand cmd = new SqlCommand("insert into struserquerysend values('" + int.Parse(x) + "','" + Session["UserName"] + "','" + get_value + "','" + dt + "','" + str_color + "')", con);
cmd.ExecuteNonQuery();

listbox having item xxx,yyy,zzz............
Ankur\m/ 22-Jan-11 1:41am    
Use 'Reply' button as I told before. I do not get any notification if you directly add a comment.

"string get_value = ListBox1.Text.ToString();"
ToString() is not required here. Text property already returns text.
As Sandeep suggested, if may not have selected any value which is why it is returning null.

1 solution

This is because Listbox does not expose a 'Text' property directly.

You need to access as: myListBox.SelectedItem.Text
I found a good article to exaplin it's use here: ListBox Control Overview[^]

In your case, there is only one item and if selected then the way to get it is from selecteditem property. In case it's not selected and yet you want to access it then use item proerty with proper index.

Have a look here for all details of listbox properties[^] exposed.

UPDATE:
Listbox.Text would give you 'Selected Value' of the list. It looks like nothing is selected and thus you are not getting anything. In that case, you need to go via item and index as I mentioned above.
 
Share this answer
 
v2
Comments
Ankur\m/ 22-Jan-11 1:24am    
"This is because Listbox does not expose a 'Text' property directly."

It does. And it will return the selected value of the control.
Ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.text.aspx
Sandeep Mewara 22-Jan-11 1:35am    
Thanks Ankur. My bad. :(
Ankur\m/ 22-Jan-11 1:42am    
Ah it's okay. I will vote now. :)
Cheers!
Sandeep Mewara 22-Jan-11 1:55am    
:) thanks man!
Rupa1 22-Jan-11 1:45am    
with out selecting we can't get listbox value.........?

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