Search code examples
c#datecastingdbnull

Check if Eval("VALUE") is null


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?


Solution

  • 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.