0
votes

Hi how can I ignore null values in a reader c# in my statement =>

 LegalDesc = reader["legal1"].ToString() + ' ' + 
             reader["legal2"].ToString() + ' ' + 
             reader["legal3"].ToString();

Scenario: if legal2 is a null value the resulting string would be

 legalDesc = legal1 + ' ' + legal3

How could apply iif used in VB?

4
How about reader["legal2"] ?? " "? - Soner Gönül
How about reader.IsDbNull? - TryingToImprove
would you re-phrase my script...thanks - Jake
LegalDesc = (reader["legal1"] ?? "") + " " + (reader["legal2"] ?? "") + " " + (reader["legal3"] ?? ""); This is what Soner meant. - Raz Megrelidze
'string' is not a valid syntax for string literals - Raz Megrelidze

4 Answers

1
votes

You could use a collection and String.Join:

List<string> legals = new List<string>();
if(!reader.IsDbNull(reader.GetOrdinal("legal1")))
    legals.Add(reader["legal1"].ToString());
if(!reader.IsDbNull(reader.GetOrdinal("legal2")))
    legals.Add(reader["legal2"].ToString());
if(!reader.IsDbNull(reader.GetOrdinal("legal3")))
    legals.Add(reader["legal3"].ToString());
LegalDesc = string.Join(" ", legals); 

Of course you could also make the code more elegant by using a custom extension method:

public static string SafeGetString(this SqlDataReader reader, int colIndex)
{
   if(!reader.IsDBNull(colIndex))
       return reader.GetString(colIndex);
   else 
       return string.Empty;
}

Now you can use:

string[] legals = { reader.SafeGetString(0), reader.SafeGetString(1), reader.SafeGetString(2) };
LegalDesc = string.Join(" ", legals.Where(s => !string.IsNullOrEmpty(s))); 

This presumes that it's actually a string column and the column ordinal positions are from 0 to 2. If that's not the case use above shown reader.GetOrdinal approach to detect them.

0
votes

Using a simple extension method:

public static string ValueOrEmpty(this object source) 
{
     return source == null ? string.Empty : source.ToString();
}

var values = new[] { reader["legal1"],reader["legal2"],reader["legal3"] };
LegalDesc = string.Join(" ", 
                        values.Select(x => x.ValueOrEmpty())
                              .Where(x => x != string.Empty));
0
votes

First

Do Not do string concatenation like that, every time you concatenate, it creates a new instance of string because string is Immutable. There is a C# class that handles appends and it's StringBuilder

So try implementing it like this

StringBuilder builder = new StringBuilder();
builder.Append(reader["legal1"])
    .Append(" ")
    .Append(reader["legal2"])
    .Append(" ")
    .Append(reader["legal3"]);
Console.WriteLine(builder.ToString());
Console.ReadKey();

This way, you don't even need to check if it's null.

0
votes

Hi I just to want rephrase my problem...

how can view a value to a LegalDesc = reader["legal1"].ToString() if legal1 is null?

I am viewing my result in a grid and so the grid shows NULL in the cell. The result should be a blank space instead of NULL.... in VB net I could use the IIf function if null then blank space...