Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Null Array

Posted on 2014-12-16
3
Medium Priority
?
104 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 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 35

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

926 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