Solved

string.isNullOrEmpty(SelectedItem.Value) not work

Posted on 2009-05-20
4
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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