Baba
Baba

Reputation: 2219

Testing date values for nulls and numbers

I am reading an excel file row by row and adding items to a collection which will be saved to the database.

In the database:

NumberValue1 to NumberValue3  are numbers and nullable.
Datevalue1 to Datavalue5 are dates and nullable.
BooleanYN1 is a varchar2(1 char) and nullable.

I want to be able to test these numbers, strings and datevalues so that am not inserting null in the database.

How can I handle this? For strings below the test should be fine. I am particular about the date variables and numbers.

if ((col2Value != null && col3Value != null & col4Value != null))
{
    excelFileDataList.Add(new ExcelData 
    {
        BusinessUnitCode = col2Value.ToString(),
        GenericJobId = Convert.ToInt32(col3Value),

        NumberValue1 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col8Value),
        NumberValue2 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col9Value),
        NumberValue3 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col10Value),

        StringValue1 = col18Value == null ? "" : col18Value.ToString(),
        StringValue2 = col19Value == null ? "" : col19Value.ToString(),
        StringValue3 = col20Value == null ? "" : col20Value.ToString(),

        DateValue1 = Convert.ToDateTime(col28Value) == null ?  : Convert.ToDateTime(col28Value),
        DateValue2 = Convert.ToDateTime(col29Value) == null ?  : Convert.ToDateTime(col29Value),
        DateValue3 = Convert.ToDateTime(col30Value) == null ?  : Convert.ToDateTime(col30Value),
        DateValue4 = Convert.ToDateTime(col31Value) == null ?  : Convert.ToDateTime(col31Value),
        DateValue5 = Convert.ToDateTime(col32Value) == null ?  : Convert.ToDateTime(col32Value),

        BooleanYN1 = col34Value == null ? "" : col34Value.ToString(),
        BooleanYN2 = col35Value == null ? "" : col35Value.ToString(),
        BooleanYN3 = col36Value == null ? "" : col36Value.ToString(),                                            
    });

I have been getting object reference not set to an instance of an object. I think this is as a result of the null values. There are null values in the excel spreadsheet for various column which is acceptable

Upvotes: 0

Views: 96

Answers (2)

Maess
Maess

Reputation: 4146

For your numbers and Dates, I suggest you use .TryParse().

var myDate;

if(DateTime.TryParse(value, out myDate))
{
   // use the value of myDate
}

Upvotes: 2

Mike Christensen
Mike Christensen

Reputation: 91608

You'll want to test for null before you call Convert.ToInt32 or Convert.ToDateTime on the object (as passing in null values will throw an exception). Instead of:

NumberValue1 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col8Value)

You'll want:

NumberValue1 = col8Value == null ? 0 : Convert.ToInt32(col8Value)

And instead of:

DateValue1 = Convert.ToDateTime(col28Value) == null ?  : Convert.ToDateTime(col28Value)

You'll want:

DateValue1 = col28Value == null ? DateTime.MinValue : Convert.ToDateTime(col28Value)

Or, if the class supports Nullable<T> values:

DateValue1 = col28Value == null ? null : (DateTime?)Convert.ToDateTime(col28Value)

Upvotes: 1

Related Questions