Solved

Null Array

Posted on 2014-12-16
3
95 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 250 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 250 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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