Question

Is there possibly a more condensed/efficient way of writing out this if/else statement? I am having to do a check for null on every single field in this database (close to 200 fields) and the code is going to look quite messy by the end. :\

if (dr["OLD_NUMBER"] != DBNull.Value)
{
    lblOldNumber.Text = dr["OLD_NUMBER"].ToString();
}
else
{
    lblOldNumber.Text = string.Empty;
}
// A bunch more with different lbls and columns
Was it helpful?

Solution 3

you could use the following:

lblOldNumber.Text = dr["OLD_NUMBER"] != DBNull.Value ? dr["OLD_NUMBER"].ToString() : string.Empty;

More information about the ?: operator instead of if, can be found here: (C# Reference)

OTHER TIPS

You can get rid of the if entirely.

DBNull.Value.ToString() returns an empty string.

You could do

lblOldNumber.Text = dr["OLD_NUMBER"] != DBNull.Value ? dr["OLD_NUMBER"].ToString() : string.Empty;

The above statement would require less lines and looks more readable to me.

As a rule of thumb when you see this sort of repetition, write a function to do your processing.

string FormatIt(object value) 
{
   return value.ToString(); // or whatever the logic is like
}

Then:

lblOldNumber.Text = FormatIt(dr["OLD_NUMBER"]);

So, if you had to modify your code to format money or smth like that, you have one place to change.

lblOldNumber.Text = string.Empty;
if (dr["OLD_NUMBER"] != DBNull.Value)
{
    lblOldNumber.Text = dr["OLD_NUMBER"].ToString();
}

or you cam make function like

void ApplyValue(Label label,object value, string defaultValue){
     label.Text =defaultValue;
     if (value != DBNull.Value)
     {
         label.Text = value.ToString();
     }
}

and use next code

ApllyValue(lblOldNumber,dr["OLD_NUMBER"],string.Empty);

Something like this:

var labels = new Dictionary<string, YourLabelClass>
                         {
                             {"OLD_NUMBER", lblOldNumber},
                             //Add your 200 fields here 
                             {"ANOTHER_NUMBER", lblAnotherNumber},
                         };

        foreach (var label in labels)
        {
            label.Value.Text = dr[label.Key].ToString();
        }

You could remove the null values from your data set when you query the database.

var result = (context.MyTable.Where(c => c.OLD_NUMBER != null));

I'd probably write a method:

void TextOrNull(object item, Label lbl)
{
  lbl.Text = item != DBNull.Value ? item.ToString() : String.Empty;
}

And call it like:

TextOrNull(dr["OLD_NUMBER"], lblOldNumber);

I would make a method that has this in, something like:

private void setLabelText( IDataRecord dr, string columnName, Label label )
{
    label.Text = string.Empty
    if (dr[columnName] != DBNull.Value)
    {
        label.Text = dr[columnName].ToString();
    }
}

Then simply call it with the appropriate labels and record names, etc.

setText( dr, "OLD_NUMBER", lblOldNumber );

I haven't experimented with this much but there is an ItemArray on the DataRow class that will return an object array of the items in the DataRow. Might be able to set up a loop and test values that way rather than hardcoding the name of the key.

Again not sure if this will work as I don't have a test scenario set up on my PC, but like this?

foreach (var col in dataRow.ItemArray)
{
    if(DBNull.Value != col)
        lbl.Text = col.ToString()
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top