问题
Quite new to C#
I need to cast a value to add minutes to a date but it can be null. Here's how I do :
if(Eval("DUREE") != DBNull.Value)
{
var duration = Convert.ToInt32(Eval("DUREE"));
var date = Convert.ToDateTime(Eval("DATE"));
var dateAsString = Convert.ToString(date.AddMinutes(duration));
DataBinder.Eval(Container.DataItem, dateAsString, "{0:HH:mm}")
}
else
{
" - "
}
Here the error I get :
DataBinding : 'System.Data.DataRowView' doesn't comport properties called : '17/04/2014 13:30:00'.
So, does the check is false? Or the error is elsewhere?
回答1:
Try use code in aspx in place where you need the string with date or " - "
<%# Eval("DUREE") == DBNull.Value || Eval("DATE") == DBNull.Value
? " - "
: Convert.ToString(Convert.ToDateTime(Eval("DATE")).AddMinutes(Convert.ToInt32(Eval("DUREE")))), "{0:HH:mm}") %>
Problem in your code that you in DataBinder.Eval
pass second parameter string with date but not property name.
回答2:
Assuming 'Eval' method is returning you a date value in form of a string, is this what you are trying to do? :
Convert.ToDateTime(Eval("DATE")).AddMinutes(Convert.ToDateTime(Eval("DUREE")).Minute)
That might be the answer to your problem for adding minutes.
However, the error that you are getting seems to indicate a different problem. The code that you have posted seems to be in code behind(?). DataBinder.Eval
method will typically be used in aspx for web control like Repeater control. In which case you will have to pass the second parameter to DataBinder.Eval
as name of a public Property.
You can check this out for clarity click here
You can do the addition of minutes while returning value of the property
来源:https://stackoverflow.com/questions/21063861/check-if-evalvalue-is-null