Solved

string.isNullOrEmpty(SelectedItem.Value) not work

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

23 Experts available now in Live!

Get 1:1 Help Now