Solved

Calculated column in gridview fails if value is null.

Posted on 2006-07-11
14
656 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
  • 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now