Solved

Calculated column in gridview fails if value is null.

Posted on 2006-07-11
14
657 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Injected data? 8 114
Remove third quote mark from widget 6 14
push logos in footer up higher 5 14
maps stopped work unsure why 7 10
Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

20 Experts available now in Live!

Get 1:1 Help Now