Sumeshk
Sumeshk

Reputation: 1988

handle null values in a datetime column with DefaultView.RowFilter in c#

Reading the contents from an xml to a datatable which is having a date column, and then filtering the datatable with DefaultView.RowFilter with certain conditions. It works perfectly with conditions except the ones with date having null values in the column Eg.

dt.DefaultView.RowFilter =" StartOn  <=#" + Convert.ToDateTime(dtpToStart.Text) + "#"

if StartOn (date field) contains any null value it shows following error message

Cannot perform '>='operation on System.String and System.DateTime.

I tried using "isnull(StartOn ,'11/11/2012')<=#"+ Convert.ToDateTime(dtpToStart.Text) +"#" but it didn't work.

Upvotes: 0

Views: 2022

Answers (1)

johnnycardy
johnnycardy

Reputation: 3046

Check for NULL before the range comparison.

For example:

dt.DefaultView.RowFilter ="StartOn IS NOT NULL AND StartOn <=#" + Convert.ToDateTime(dtpToStart.Text) + "#"

Upvotes: 2

Related Questions