Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 663
  • Last Modified:

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.                    
0
Break40
Asked:
Break40
  • 9
  • 4
1 Solution
 
davbouchardCommented:
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" />
<% } %>
0
 
davbouchardCommented:
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" />
<% } %>
0
 
Break40Author Commented:
I get an "ISNULL" is not declared error.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
davbouchardCommented:
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#)
0
 
Break40Author Commented:
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.
0
 
Break40Author Commented:
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.
0
 
davbouchardCommented:
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.
0
 
Break40Author Commented:
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."
0
 
Break40Author Commented:
I tried to test them inside the function, but it would not pass them if they were empty, I would get a casting error.
0
 
Break40Author Commented:
Can I test the column of the gridview to see if it's empty?
0
 
valrogCommented:
How about making the fields optional in the function, and then test for them.  It should pass even if all are blank.
0
 
Break40Author Commented:
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.
0
 
Break40Author Commented:
<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.
0
 
Break40Author Commented:
I solved it another way, but thanks for the help and sugestions that lead to the solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now