Check if Eval(“VALUE”) is null

倖福魔咒の 提交于 2019-12-13 04:35:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!