Why does a field not create a SQL statement value of single quotes if I assign spaces to it in C# using VS2010?

I am developing a C# application using VS2010.

I have the attached input file that I am using to read records from. There is only 1 record in this input file.

My application builds up a SQL Statement based on the input fields that are read by my application but I don't understand why the 8th field (field titled recordnum) does not generate single quotes in the SQL Statement that is built since there is a null value in the input field. The first field (bankNum) does not have a value in the input file yet it generated single quotes in the SQL Statement as I suspected it would.

To recap, the 1st and 8th field both have no value yet the 1st field is converted to single quotes in the build SQL Statement while the 8th field does not generate single quotes. The output is blank (not single quotes) for the 8th field and thus creates an erroneous SQL Statement.

INSERT INTO tbl_SMR_OI_DRS (bankNum, transDtYR, transDtMnth, transDtday, officeNum, checkNum, transType, recordNum, processDTYear) VALUES ('    ', 2013, 08, 21, '007', '211503', 'TTT',     , '2013')

Do you know how I can resolve this issue?

A snippet of my code is as follows:

                    sArray = new string[9];
                    sArray[0] = bankNum.ToLower() == "null" ? " ": bankNum;
                    sArray[1] = transDtYR;
                    sArray[2] = transDtMnth;
                    sArray[3] = transDtday;
                    sArray[4] = officeNum;
                    sArray[5] = checkNum;
                    sArray[6] = tranType;
                    sArray[7] = recordNum.ToLower() == "null" ? " ": recordNum;
                    sArray[8] = processDTYear;
                    sb.AppendFormat(myInsert, sArray);
                    string cmdText = sb.ToString();
OI-DRS.txt
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.

Miguel OzSoftware EngineerCommented:
Please post your SQL table creation ( I would like to see column types and check columns(bankNum, recordNum) are  marked "Allow null".)
0
zimmer9Author Commented:
.........................Data Type                  Allow Nulls    
                               -------------                  ---------------      
   
bankNum                nvarchar(4)                checked (selected)
   
recordNum             nvarchar(4)                checked (selected)
0
Miguel OzSoftware EngineerCommented:
The simplest way to solve it, if your issue is only the 8th field:  Modify your insert sql as follows:
INSERT INTO tbl_SMR_OI_DRS (bankNum, transDtYR, transDtMnth, transDtday, officeNum, checkNum, transType, recordNum, processDTYear) VALUES ('    ', 2013, 08, 21, '007', '211503', 'TTT', '', '2013')
Notice that I am adding a '' to add an empty string. Your C# code will read as empty.

If you have null values already then you need to check for null values as shown in the links below:
http://msdn.microsoft.com/en-us/library/sksw8094.aspx
http://stackoverflow.com/questions/218808/c-sharp-ado-net-nulls-and-dbnull-is-there-more-efficient-syntax

for your case:
sArray[7] = recordNum.HasValue ?  recordNum: " ";
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
zimmer9Author Commented:
Error      1      'string' does not contain a definition for 'HasValue' and no extension method 'HasValue' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)      \\msad\root\na\ny\users\zimmermj\visual studio 2010\Projects\SMRWebForm\SMRWebForm\SMRWebForm1.aspx.cs      119      43      SMRWebForm
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.