473,467 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

c# getting selected value from list box

210 Recognized Expert New Member
I have a list box that has a list of roles defined. List of roles are binded to the list box in page load event. I am trying to retrieve the selected role from the text box but i am getting a null reference error even though one of the item is selected. below is the code that gives me null reference.


Expand|Select|Wrap|Line Numbers
  1.  String role = lstRole.SelectedItem.ToString(); 
Expand|Select|Wrap|Line Numbers
  1.  Label1.Text = role;

I even tried using the code below and all i get is null value. seems like it not selecting any thing form the list box.

Expand|Select|Wrap|Line Numbers
  1. String role = lstRole.SelectedValue;
Does any one have any idea what is going on??

Thanks in advance.
Feb 7 '08 #1
4 17783
harshmaul
490 Recognized Expert Contributor
hi,
please show me how you are binding the data to the list.
A common problem i have is that on the page load it binds the data again therefore clearing the selection i made.
Feb 7 '08 #2
kunal pawar
297 Contributor
Please used property
selectedItem.Text for text and SelectedItem.value for getting values
Feb 8 '08 #3
semomaniz
210 Recognized Expert New Member
Expand|Select|Wrap|Line Numbers
  1. if (!Roles.IsUserInRole(User.Identity.Name, "Admin"))
  2.             {
  3.                 btnSubmit.Enabled = false;
  4.                 btnclear.Enabled = false;
  5.                 lblerr.Text = " You donot have permission to create accounts ";
  6.  
  7.             }
  8.             else
  9.             {
  10.  
  11.                 lstRole.DataSource = Roles.GetAllRoles();
  12.                 lstRole.DataBind();
  13.             }
  14.  
This is how i am binding the data to the list box. I tried using

Expand|Select|Wrap|Line Numbers
  1.  Label1.text = lstRole.SelectedItem.Text
it still gives me null reference exception.
Feb 8 '08 #4
semomaniz
210 Recognized Expert New Member
got the problem solved. I forgot to check for post back. That was the reason why the value selected was being lost . Thank you for all your help.

cheers!!!!!!!!!!!!
Feb 8 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
3
by: luisg | last post by:
I am having some difficulty getting a checkboxlist control to determine which checkboxes are checked. Here is a code sample from the event that should read it: private void...
5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
10
by: aroraamit81 | last post by:
I have a string like "English,Japanese,Spanish" and a list box which contains say 10 lang. Now I want my List box should come by selected values as the values in string and rest of the values to...
6
by: rishabhshrivastava | last post by:
Hey All, Can someone suggest me a way to get the values of CheckBox(es) selected in a CheckBoxList control using JAVASCRIPT. I am pasting my current code gere but its not working need some...
1
by: Roffers | last post by:
Okay I have a parent page, we'll call it parent.aspx and it holds the following two user controls <uc1:Box1 id="Box1" runat="server"></uc1:Box1<----THIS CONTAINS A DROP DOWN LIST <br>...
4
by: Brett Wesoloski | last post by:
I am having a bit of a problem getting the current value of the checkbox list. I have tried using the selected value as I thought that would give me the current value of the check box but it...
4
by: jeet | last post by:
Plz help me.Problem is that On the first page I display all other user with checkbox and give user option to select only two user which he wants to send message. Tell me please how I'll get those...
45
by: angelicdevil | last post by:
i have 2 tables 1 is status_type with field name status and other is users with field username and status now i want that the first listbox lists all status from status type ( this i have achieved...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.