1

I get data from a table but the 'DateOfBirth' field is shown as null. So if 'DateOfBirth' is null how can I skip it?

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

var dd = dx.Tables[0].AsEnumerable().Select(x => new StaffModel
{
    userno = x.Field<string>("USERNO"),
    dateofbirth = x.Field<DateTime>("DATEOFBIRTH"), // This comes as a DB NULL.
    passportno = x.Field<String>("PASSPORTNO"),
    passportexp = x.Field<DateTime>("EXPDATE")
});

4 Answers 4

1

Try to check for DBNull value in this way:

var dd = dx.Tables[0].AsEnumerable().Select(x => new StaffModel
{
    userno = x.Field<string>("USERNO"),
    dateofbirth = x["DATEOFBIRTH"] != DBNull.Value ? x.Field<DateTime>("DATEOFBIRTH") : DateTime.MinValue,
    passportno = x.Field<String>("PASSPORTNO"),
    passportexp = x.Field<DateTime>("EXPDATE")
});

If value is equal to DBNull.Value then set MinValue or whatever you want.

Sign up to request clarification or add additional context in comments.

2 Comments

DateTime.MinValue;
Fix my fault. Thanks :-)
0

Use a nullable DateTime; DateTime?. In your StaffModel:

public class StaffModel
{
    public string userno { get; set; }
    public DateTime? dateofbirth { get; set; }
    public string passportno { get; set; }
    public DateTime passportexp { get; set; }
}

Then when filling that model:

var dd = dx.Tables[0].AsEnumerable().Select(x => new StaffModel
{
    userno = x.Field<string>("USERNO"),
    dateofbirth = x.Field<DateTime?>("DATEOFBIRTH"), // note the ?        
    passportno = x.Field<String>("PASSPORTNO"),
    passportexp = x.Field<DateTime>("EXPDATE")
});

4 Comments

Date of birth is not a nullable type.in my database there's a issue, for the time being i need to skip it
Date of birth is not a nullable type Your exception would appear to say otherwise.
actually DB coming from outside,it's a DB issue... for the time being i need to skip that..
Either way the solution is the same. You cannot supply null to a DateTime - only DateTime?. If you want to skip working with that value you would need to add some logic to the code which deals with your resulting IEnumerable<StaffModel>
0

You can check for DBNull.Value

object value = x.Field("DATEOFBIRTH");
if (value == DBNull.Value)
{
    // do something
}  
else
{
    // do something else
}

Comments

0

You need to assign DateTime.MinDate if your variable is not null type.

var dd = dx.Tables[0].AsEnumerable().Select(x => new StaffModel
{
    userno = x.Field<string>("USERNO"),
    dateofbirth = x.Field<DateTime>("DATEOFBIRTH") ?? DateTime.MinDate, 
    passportno = x.Field<String>("PASSPORTNO"),
    passportexp = x.Field<DateTime>("EXPDATE")
});

Comments

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.