3

I am facing one problem in c# datetime i have trying more than time. it's not giving a problem solution. so give me one solution.

var accommodationcategoryList = EmployeeAttendanceCacheMaster.GetAllEmployeeAttendance();
DataSourceResult result = accommodationcategoryList.ToDataSourceResult(request);

foreach(var item in accommodationcategoryList)
{
    item.User = UserCacheMaster.GetUserById(item.employeeid);
    item.loginTime = item.login.Value.ToShortDateString();
    item.logoutTime = (item.logout.Value.ToShortDateString() != null) ? item.logout.Value.ToShortDateString() : "-";
}

My problem is not showing the logouttime it showing error looks like

nullable object must have a value

1
  • 2
    Surely you meant to use item.logout.HasValue instead Commented Apr 18, 2016 at 11:15

2 Answers 2

6

You're trying to check if a conversion against the null object is null rather than checking the nullable object itself. You need to change this line:

item.logoutTime = (item.logout.Value.ToShortDateString() != null) ? item.logout.Value.ToShortDateString() : "-";

to

item.logoutTime = item.logout.HasValue ? item.logout.Value.ToShortDateString() : "-";
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

if(item.logout.HasValue) {
    item.logoutTime = item.logout.Value;
}

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.