Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Null Array

Posted on 2014-12-16
3
Medium Priority
?
102 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
[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
3 Comments
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 1000 total points
ID: 40503529
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 34

Accepted Solution

by:
it_saige earned 1000 total points
ID: 40503654
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
ID: 40504947
Thank you both for your assistance.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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