0

i have the following code:

var distinctRows = 
      (from row in ds.GetTable(this.TagShortName).AsEnumerable()
         select new Audit    
          {
             ID =row.Field<long>("ID"),
             UpdatedBy =string.Format("{0} {1}", 
                                      row.Field<string>("UpdatedByFirstName"),
                                      row.Field<string>("UpdatedByLastName")),
             UpdatedOn = ow.Field<DateTime>("UpdatedOn")
          }).Distinct(new AuditComparer());

when i have null value in UpdateOn field it throw exception on:

UpdatedOn =row.Field<DateTime>("UpdatedOn")

exception: Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type.

how to handle this?

1
  • 1
    Well what is UpdatedOn datatype you will need that to be DateTime? Commented Apr 11, 2013 at 9:00

2 Answers 2

1

Make UpdatedOn nullable or

UpdatedOn = row.Field<DateTime?>("UpdatedOn")?? DateTime.MinValue
Sign up to request clarification or add additional context in comments.

Comments

0

Use Nullable<DateTime> or DateTime?

UpdatedOn = row.Field<DateTime?>("UpdatedOn")

4 Comments

row.Field<DateTime?>("UpdatedOn") not working i already try this. how to use Nullable<DateTime> in my case?
@NomiAli, Nullable<DateTime> is same as DateTime?, the later is just syntactic suger, you need to make sure that your UpdatedOn is also defined as DateTime?, or you need to check it against null and then assign UpdatedOn some default value.
Make sure UpdatedOn defined Nullable in entity data model.
thanks man! actually my UpdatedOn was not nullable in entity. Thanks for the answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.