How to assign a value of spaces to a field with a value of Null in C# using VS2010?

I am developing a C# application and want to store a value of spaces in a field if the field has a value of null.

The assigment of spaces can take place at one of 2 places in my code as follows:

Do you know how to assign a value of spaces to the field "bankNum"
if the field has a null value?

1)   during the Substring process:

List<string> ReadFile = File.ReadAllLines(@"U:\SnrMgtRpt\OI_DRS.txt").ToList();

foreach (string line in ReadFile)
{
    bankNum = line.Substring(0, 4);

OR

2)    during the array assignment process:
sArray = new string[9];
sArray[0] = bankNum;
zimmer9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Are you talking "NULL" or are you talking null?

There's a difference one being a string representing null and one being null itself.

I would recommend doing it here:
sArray[0] = bankNum.ToLower() == "null" ? <your value here> : bankNum;

this is shorthand for
if (bankNum.ToLower() == "null")
   sArray[0] = <your value>;
else
   sArray[0] =   bankNum;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Daniel Van Der WerkenIndependent ConsultantCommented:
I'm not 100% sure what you're trying to do. Are you trying to insert a ' ' into the database if the string is null? Like

if (string.IsNullOrEmpty(bankNum))
{
   sArray[0] = ' ';
}
else
{
   sArray[0] = bankNum;
}

Open in new window


You know, if you wanted to, you could do the following with the insert. Before, in the other answer, I suggested the following:

const string myInsert = "INSERT INTO TABLE ( STR_COL1, INT_COL2, STR_COL3 ) VALUES ( '{0}', {1}, '{2}')";

Open in new window


Well, we used '{0}' to insert a string. However, you could simply do {0} without the single quote marks. Like this:

const string myInsert = "INSERT INTO TABLE ( STR_COL1, INT_COL2, STR_COL3 ) VALUES ( {0}, {1}, {2})";

Open in new window


Then, write a little method that checks for empty strings or nulls and writes "NULL" out:

string CheckString(string inString)
{
   string outString = string.Empty;
   if (string.IsNullOrEmpty(inString))
   {
      outString = "NULL";
   }
   else
   {
     outString = "'" + inString + "'";
   }
   return outString;
}

Open in new window


Now, when you make the sArray, you do this:

sArray[0] = CheckString(bankNum);

Open in new window


it will return either NULL or 'valueOfBankNum' (both in quotes but I'm leaving out the quotes to show what they really are. Therefore, really "NULL" and "'valueOfBankNum'".

Because the const string doesn't have the single quotes around the {0} and such any longer, you don't need to worry about writing it out incorrectly. If the actual bankNum value is null, and you want to write NULL to the database, then it will insert NULL. If it's a string, it will insert 'valueOfBankNum' just like you want.

Make sense?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.