Link to home
Start Free TrialLog in
Avatar of David Pickart
David PickartFlag for United States of America

asked on

Calculated column in gridview fails if value is null.

I have a gridview to display work days of the week.  Each record as 4 fields for used to collect time values.  I have a function that can automatically determine number of hours that an individual worked based on the entries they make.  I use a template item in the gridview and call a function like so:

<asp:Label ID="Label5" Text = '<%#Get_Travel_Hours(Eval("StartTime"), Eval("StoreArrival"), Eval("StoreDepart"), Eval("EndofDay")) %>' runat="Server" />--%>

What I need to know is how to skip this code if any of the 4 fields are empty.                    
Avatar of davbouchard
davbouchard

Try something like this

If you still need the control:

<% If( IsNull(Eval("StartTime")) or IsNull(Eval("StoreArrival")) or IsNull(Eval("StoreDepart")) or IsNull(Eval("EndofDay")) ){ %>
  <asp:Label ID="Label5" Text = '' runat="Server" />
<% }else{ %>
  <asp:Label ID="Label5" Text = '<%#Get_Travel_Hours(Eval("StartTime"), Eval("StoreArrival"), Eval("StoreDepart"), Eval("EndofDay")) %>' runat="Server" />
<% } %>

If you don't need it at all:

<% If( Not (IsNull(Eval("StartTime")) or IsNull(Eval("StoreArrival")) or IsNull(Eval("StoreDepart")) or IsNull(Eval("EndofDay"))) ){ %>
  <asp:Label ID="Label5" Text = '' runat="Server" />
<% }else{ %>
  <asp:Label ID="Label5" Text = '<%#Get_Travel_Hours(Eval("StartTime"), Eval("StoreArrival"), Eval("StoreDepart"), Eval("EndofDay")) %>' runat="Server" />
<% } %>
Sorry Mistake there

If you don't need it at all:

<% If( Not (IsNull(Eval("StartTime")) or IsNull(Eval("StoreArrival")) or IsNull(Eval("StoreDepart")) or IsNull(Eval("EndofDay"))) ){ %>
  <asp:Label ID="Label5" Text = '<%#Get_Travel_Hours(Eval("StartTime"), Eval("StoreArrival"), Eval("StoreDepart"), Eval("EndofDay")) %>' runat="Server" />
<% } %>
Avatar of David Pickart

ASKER

I get an "ISNULL" is not declared error.
Try

<% If( Not (Eval("StartTime") is null or Eval("StoreArrival") is null or Eval("StoreDepart") is null or Eval("EndofDay") is null) ){ %>
  <asp:Label ID="Label5" Text = '<%#Get_Travel_Hours(Eval("StartTime"), Eval("StoreArrival"), Eval("StoreDepart"), Eval("EndofDay")) %>' runat="Server" />
<% } %>

I think it depends on your page language (VB, C#)
I am using VB.  ISNULL is no longer supported.  i changed it to the following.

 <% If (IsDBNull("StartTime") Or IsDBNull("StoreArrival") Or IsDBNull("StoreDepart") Or IsDBNull("EndofDay")) Then%>
                    <asp:Label ID="Label5" Text = '' runat="Server" />
                    <% Else%>
                    <asp:Label ID="Label6" Text = 'all empty' runat="Server" />
             <%--       <asp:Label Text = '<%#Get_Travel_Hours(Eval("StartTime"), Eval("StoreArrival"), Eval("StoreDepart"), Eval("EndofDay")) %>' runat="Server" />--%>
                    <%  End If%>

It seems to work but all rows in datagrid are evaulating to "all empty" even if all 4 fields have a time in them.
I removed the eval due to an error regarding binding.  The above will not evaluate coreectly because I am just using a string.  I need to find a better way.
The way you did it, it goes to the TRUE part if any of the 4 field is empty and if none of them are empty it outputs 'all empty'.

Do you mean that every row displays 'all empty', which is wrong because you have some empty?

Something else you could do is test the parameters when you are inside the function Get_Travel_Hours and return '' if one of them is empty.
A line like  <% If (Eval("StartTime") < "") Then%>

Produces the error

"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
I tried to test them inside the function, but it would not pass them if they were empty, I would get a casting error.
Can I test the column of the gridview to see if it's empty?
ASKER CERTIFIED SOLUTION
Avatar of valrog
valrog

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I need to check them before I call the function, mainly because if they are empty they fail before and after the function is called.
<asp:label runat="server"
     Text='<%# DataBinder.Eval(Container.DataItem,
          "StartTime") %>'
   />

This will place empty string in label, but when I try to use it inside an if statement the Container becomes unrecognized.
I solved it another way, but thanks for the help and sugestions that lead to the solution.