How to avoid error when reading DateTime filed from DataTable that don't have a value

Hi,

I keep running into trouble with my DataTable that has two DateTime columns that sometimes do not have a value in them.

If I try to get the value as DateTime, I get a cast error, when I try to get the value as an object, I get a nullReferenceException when I try to access it later.

Can anyone help please, there must be some good practice way of handling this (and I don't like the "object approach" anyway).

While writing this, I came to think that maybe a try...catch would help since if one or both dates are missing, I don't want to update the ElapsedDays field anyway. But there must be a nicer way of handling this - or?
private void AddNonWorkDays(string startCol, string endCol, DataTable dt)
		{
			// Add the new column...
			//DataColumn dc = new DataColumn("DateExample",System.Type.GetType("System.DateTime"));
			DataColumn dc = new DataColumn();
			dc.ColumnName = "Elapsed Days";
			dc.DataType = typeof(System.Int32);
			
			
			dt.Columns.Add(dc);
 
			// Calculate the values for the new column for each row...
 
			List<DateTime> holidays = new List<DateTime>();
 
			foreach (DataRow row in dt.Rows)
			{
				DateTime startDate = row.Field<DateTime>("Case Open Date");
				// The following line causes an error when there are no data...
				// {"Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type."}
				// DateTime endDate = row.Field<DateTime>("Date/Time Resolved w/Customer");
 
				// Tried to workaround this by using the object type which avoids the error here...
				object endDate = row.Field<object>("Date/Time Resolved w/Customer");
				// ...and then checking if the value is empty...
				if (endDate.ToString() != string.Empty)
				{
					// ...but that causes an error here at (DateTime)endDate...
					// nullReferenceException
					int duration = CountWorkDays(startDate, (DateTime)endDate, holidays);
 
					row.SetField<Int32>("ElapsedDays", duration);
				}
 
			}
 
		}

Open in new window

Sharp2bAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jinalCommented:
Avoid use object.

Use nullabletype.

Please provide detail of countworkdays.
0
Sharp2bAuthor Commented:
Thanks,
you made a similar comment to my other case yesterday (http://www.experts-exchange.com/Q_24536961.html) Ddo you mean that this entire line should be in?
data.Field<DateTime?>("Case Open Date") != null  nullable type

The compiler didn't like it and I didn't understand it but maybe you can explain and I would very much like to try as it sounds like a better approach.
I got the countworkdays from another request here: http://www.experts-exchange.com/Q_24535798.html
also attached below
private int CountWorkDays( DateTime startDate, DateTime endDate, List<DateTime> excludedDates )
{
    int dayCount = 0;
    int inc = 1;
    bool endDateIsInPast = startDate > endDate;
    DateTime tmpDate = startDate;
    DateTime finiDate = endDate;
 
    if( endDateIsInPast )
    {
        // Swap dates around
        tmpDate = endDate;
        finiDate = startDate;
 
        // Set increment value to -1, so it DayCount decrements rather 
        // than increments
        inc = -1;
    }
 
    while( tmpDate <= finiDate )
    {
        if( !excludedDates.Contains( tmpDate ) )
        {
            dayCount += inc;
        }
 
        // Move onto next day
        tmpDate = tmpDate.AddDays( 1 );
    }
 
    return dayCount;
}

Open in new window

0
jinalCommented:
Hello,

what is value of end date in case of enddate is null ?

I means to say that when DataTable return null value for enddate which value used in CountDays for that. Is that current date is used for that.
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Sharp2bAuthor Commented:
Hi and thanks for coming back to me.

Sorry for the delay, I suppose we are in different time zones.

Maybe it's not clear from the code as this is still a first effort but my intention is not to call CountWorkDays() in case of either startDate or endDate don't have a value in the DataTable. In that case, I will also leave the "ElapsedDays" field empty as this should not be included in the further calculations anyway.

Maybe it's possible to check for null values in the DataTable before retrieving the values? If the field is null, I don't want the value anyway so I could just skip this and move on to the next record.

Currently I try to get the value and then check for null but maybe it's better to do it the other way around. If I can figure out how, that is. I have seen somethign like DBNull, maybe that would work, I need to test.
0
g_johnsonCommented:
I think you should just check for the null

if (startdate != null)
{
     //do your thing
}
0
Sharp2bAuthor Commented:
But I can't as when I try to retrieve the startDate and there is no value in the DB, I get an error. I think this is because startDate or endDate can't be null.

I need to test this once again now that I read a little bit more but it looks like it doesn't help declaring these as DateTime? startDate to make it nullable as it fails already on this part:
row.Field<DateTime>("Case Open Date")
I will try again with:
row.Field<DateTime?>("Case Open Date")
but that didn't help in a previous case.
0
g_johnsonCommented:
What about

if (row.Field<DateTime>("Date/Time Resolved w/Customer") != null)
{
     //do your thing
}

I'm just guessing here ...
0
jinalCommented:
When you work with NullableType

<DateTime?>dt = null;

if(dt.HasValue) // This will indicate that Nullable Type contain value
{
   CallFunction(dt.Value);  // This contain value for date.
}
else
{
  // Do Nothing
}

0
jinalCommented:
       DataTable dt = new DataTable();
        dt.Columns.Add("Case Owner");
        dt.Columns.Add("Case Open Date",typeof(DateTime));
        dt.Columns.Add("Date/Time Resolved w/Customer",typeof(DateTime));
        DataRow dr = dt.NewRow();
        dr["Case Owner"] = "Test1";
        dr["Case Open Date"] = DateTime.Now.AddDays(-15).Date  ;
        dr["Date/Time Resolved w/Customer"] = DateTime.Now;
        dt.Rows.Add(dr);
       
        dr = dt.NewRow();
        dr["Case Owner"] = "Test2";
        dr["Case Open Date"] = DateTime.Now.Date;
        dr["Date/Time Resolved w/Customer"] = DBNull.Value;
        dt.Rows.Add(dr);

        foreach (DataRow dr1 in dt.Rows)
        {
            DateTime? dtStart = dr1.Field<DateTime?>("Case Open Date");
            DateTime? dtend = dr1.Field<DateTime?>("Date/Time Resolved w/Customer");

            if (dtStart.HasValue && dtend.HasValue)
            {
                CountDay(dtStart.Value, dtend.Value);
            }

        }


 private TimeSpan CountDay(DateTime dtStart, DateTime dtEnd)
    {
        return dtEnd - dtStart;
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jinalCommented:
Hello ,

In my previous comment  . I just take sample and use your own way. That might help.
0
Sharp2bAuthor Commented:
Thanks a lot!
Unfortunately some other things came up over the weekend so I didn't get a chance to try this. I will try to check this as soon as possible.

Again, thanks a lot for your efforts.
0
Sharp2bAuthor Commented:
Sorry I didn't get it at first. I now see that I need to use two "?" operators in the line: DateTime? dtStart = dr1.Field("Case Open Date");
DateTime? is a different type to DateTime and I need to be consistent.

Thanks a lot, I really appreciate all your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.