0

I'm trying to test for null values of two particular fields, and if they're null, replace the whitespace with the amount of 0s appropriate for that field. My code below gives no errors, but isn't doing anything and the normal values are being used.

var replace1 = "00";
var replace2 = "0";
var downpay = _Reader[26].ToString();
var preauth = _Reader[28].ToString();

if(downpay == null || downpay == "  ") { downpay = replace1; }
if(preauth == null || preauth == " ") { preauth = replace2; }

  writer.WriteLine("{0}{1}{2}{3}{5}{6}{7}{15}{16}{17}{18}{19}{20}    0{21}{22}{23}{24}{25}{26}{27}{28}{29}{30}{31}{32}{33}{34}{35}{36}{37} {38}{39}{40}{41}{42}{43}{44}{45}{46}{47}{48}{49}       {50}", _Reader[1], _Reader[2], _Reader[3], _Reader[4], _Reader[5], _Reader[6], _Reader[7], _Reader[8], _Reader[9], _Reader[10], _Reader[11], _Reader[12], _Reader[13], _Reader[14], _Reader[15], _Reader[16], _Reader[17], _Reader[18], _Reader[19], _Reader[20], _Reader[21], _Reader[22], _Reader[23], _Reader[24], _Reader[25], downpay, _Reader[27], preauth, _Reader[29], _Reader[30], _Reader[31], _Reader[32], _Reader[33], _Reader[34], _Reader[35], _Reader[36], _Reader[37], _Reader[38], _Reader[39], _Reader[40], _Reader[41], _Reader[42], _Reader[43], _Reader[44], _Reader[45], _Reader[46], _Reader[47], _Reader[48], _Reader[49], _Reader[50], date3);

Update: Fixed the issue using the debugger. The values were \00 and \0, even though when i copy from the field in sql server it's empty.

  • 3
    if this is a SqlReader, you'll need to test for [`DbNull`](http://stackoverflow.com/questions/1772025/sql-data-reader-handling-null-column-values). Also, if you want to test for empty and whitespace, why not use `string.IsNullOrWhiteSpace`? – StuartLC Nov 25 '14 at 16:23
  • 1
    @StuartLC the types of the variables are `string`, they couldn't be `DBNull`. – Servy Nov 25 '14 at 16:23
  • It sounds like you condition simply isn't true. What is the *actual* value of these values here? – Servy Nov 25 '14 at 16:24
  • 2
    Please learn how to use the debugger--this type of problem is easily solved by using the debugger. – Polyfun Nov 25 '14 at 16:28
  • @StuartLC That still assumes that `_Reader[26]` is `DBNull`. There is no indication that that's the case. It could be any number of things. All that we know is that it's not `null` or `" "`. It could be literally anything else. – Servy Nov 25 '14 at 16:44

0 Answers0