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.