tags:

views:

34

answers:

2

How can we handle the case where one date is blank in DateDiff method? Example:

DateDiff(DateInterval.Day, CType(txt61_2.Text, Date), CType(txt21_2.Text, Date))

In above statement fields txt61_2.Textor txt21_2.Text may be empty then take it has 0

Please let me know how to manage this?

A: 

There is no default conversion from "" to a datetime so a InvalidCastException occurs. There is no way to "handle" this occurrence as it must be defined by your business logic. Assuming that you are using this in a search field you could do something like this.

if(string.isnullorempty(txt21_2.Text))then
   date1 = DateTime.MaxValue;
end if 
rerun
A: 

I would suggest looking at DateTime.TryParse/DateTime.Parse and handling the exceptions thrown in you business logic as rerun says.

Monkieboy