Solved

Calculated column in gridview fails if value is null.

Posted on 2006-07-11
14
660 Views
Last Modified: 2011-09-20
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
Comment
Question by:Break40
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
14 Comments
 
LVL 6

Expert Comment

by:davbouchard
ID: 17082867
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
 
LVL 6

Expert Comment

by:davbouchard
ID: 17082876
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
 

Author Comment

by:Break40
ID: 17083128
I get an "ISNULL" is not declared error.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 6

Expert Comment

by:davbouchard
ID: 17083308
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
 

Author Comment

by:Break40
ID: 17083448
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
 

Author Comment

by:Break40
ID: 17083489
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
 
LVL 6

Expert Comment

by:davbouchard
ID: 17083580
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
 

Author Comment

by:Break40
ID: 17083587
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
 

Author Comment

by:Break40
ID: 17083598
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
 

Author Comment

by:Break40
ID: 17083608
Can I test the column of the gridview to see if it's empty?
0
 
LVL 3

Accepted Solution

by:
valrog earned 250 total points
ID: 17084022
How about making the fields optional in the function, and then test for them.  It should pass even if all are blank.
0
 

Author Comment

by:Break40
ID: 17084209
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
 

Author Comment

by:Break40
ID: 17085316
<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
 

Author Comment

by:Break40
ID: 17138257
I solved it another way, but thanks for the help and sugestions that lead to the solution.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question