Solved

string.isNullOrEmpty(SelectedItem.Value) not work

Posted on 2009-05-20
4
407 Views
Last Modified: 2013-12-17
Hi

I have a drop down list in a aspx form which uses C#.

If I do not choose anything and pressed submit button, it throws nullreference exception.

But, I use the following code to check it in if condition and still not work.

How can I fix it?
string a = this.location.SelectedItem.Value;

if (string.IsNullOrEmpty(this.location.SelectedItem.Value) || this.location.SelectedItem.Value == null || this.location.SelectedItem.Value =="")

Open in new window

0
Comment
Question by:techques
4 Comments
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 25 total points
ID: 24429157
Hi techques,
Try this easier way:

string a = this.location.SelectedItem.Value.toString();
if(a=="")

0
 
LVL 9

Assisted Solution

by:tetorvik
tetorvik earned 25 total points
ID: 24429254
i believe your dropdownlist SelectedItem is null, therefore you'll get the exception. You'll need to check that prior to access it's Value property

if(this.location.SelectedItem != null)
{

}
0
 

Author Closing Comment

by:techques
ID: 31583380
both work, thanks for help
0
 
LVL 8

Expert Comment

by:ppittle
ID: 24431941
Or try this.location.SelectedValue, which returns a string, so you don't need to check for a null reference, as long as you have a valid pointer to your drop down list
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now