Solved

Null Array

Posted on 2014-12-16
3
90 Views
Last Modified: 2014-12-17
I have a value that I am splitting into an array. When the value is null, it gives me an error message. How can I prevent the error message when the value is null.
                        string PhoneNum = reader["PhoneNumber"].ToString();
                        string[] strArr = PhoneNum.Split('-');
                        PersonPhoneNumber1.Value = strArr[0];
                        PersonPhoneNumber2.Value = strArr[1];
                        PersonPhoneNumber3.Value = strArr[2];   

Open in new window

0
Comment
Question by:RecipeDan
3 Comments
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
Comment Utility
I assume PhoneNum is empty.
If it is, please modify code to check for empty as follows:
  string PhoneNum = reader["PhoneNumber"].ToString();
  if (!String.IsNullOrEmpty(PhoneNum))
  {
	String[] Strarr = Phonenum.Split('-');
	Personphonenumber1.Value = Strarr[0];
        if (Strarr.Length > 1) 
	     Personphonenumber2.Value = Strarr[1];
        if (Strarr.Length > 2) 
	     Personphonenumber3.Value = Strarr[2];
  }
 //else set default values

Open in new window

0
 
LVL 32

Accepted Solution

by:
it_saige earned 250 total points
Comment Utility
Another possibility is that reader["PhoneNumber"] represents a null value in which case you would use the following in conjunction with Miguels code:
string PhoneNum = reader["PhoneNumber"] != null ? reader["PhoneNumber"].ToString() : string.Empty;
if (!String.IsNullOrEmpty(PhoneNum))
{
	String[] Strarr = Phonenum.Split('-');
	Personphonenumber1.Value = Strarr[0];
	if (Strarr.Length > 1) 
		Personphonenumber2.Value = Strarr[1];
	if (Strarr.Length > 2) 
		Personphonenumber3.Value = Strarr[2];
}
//else set default values

Open in new window


-saige-
0
 
LVL 1

Author Comment

by:RecipeDan
Comment Utility
Thank you both for your assistance.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

21 Experts available now in Live!

Get 1:1 Help Now