Solved

string.isNullOrEmpty(SelectedItem.Value) not work

Posted on 2009-05-20
4
409 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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