I'm unable to format a label with a databind label?

I need this label to either show a date from the database, or if it is Null, then to say "open".

<td>
 <asp:Label ID="End_DateLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "End_Date", "{0:d;open;Open}") %>' NullDisplayText="Open" />
</td>

I can do this in a asp:grideview with: NullDisplayText="Open", but don't see how it works in the label of asp:formview.
ocdcAsked:
Who is Participating?
 
Seven priceConnect With a Mentor Full StackCommented:
Another example I have.

 <asp:Panel ID="Panel5" runat="server" visible='<%# IIf(Eval("TotalCase") Is DbNull.Value, "False","True") %>'>
                          <%#IIf(Format(Eval("Cflag")) & String.Empty = ("0"), (Eval("TotalCase", "<u>Total Cases Available</u>" & "&nbsp;<font color=red>( " & "{0}" & " )</font>")), (Eval("TotalCasesava", "&nbsp;")))%>
                          <!-- gp purpose -->
                          <%#IIf(Format(Eval("Cflag")) & String.Empty = ("1"), (Eval("TotalCase", "<u>Total Pallets Available</u>" & "&nbsp;<font color=red>( " & "{0}" & " )</font>")), (Eval("TotalCasesava", "&nbsp;")))%>
 </asp:Panel>
0
 
Seven priceFull StackCommented:
The best way is to use a panel control within your grid.  for example

<asp:Panel ID="Panel4" runat="server" visible='<%# IIf(Eval("Info") Is DbNull.Value, "False","True") %>'> <%#DataBinder.Eval(Container.DataItem, "Info")%></asp:Panel></div></div>
        <asp:Panel ID="Panel3" runat="server" Visible='<%# IIf(Eval("Date") Is DbNull.Value, "False","True") %>'>
                                <asp:Label ID="Label4" runat="server" Text="Date"></asp:Label>
                             </asp:panel>
0
 
lazyberezovskyConnect With a Mentor Commented:
The syntax of format is {0:positive;negative;zero}.
So, you cannot use it with null value.
<asp:Label ID="End_DateLabel" runat="server" Text='<%# Eval("End_Date") == null ? "Open" : Eval("End_Date", "{0:d}")%>' />

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Seven priceFull StackCommented:
>>>>>> Question: I need this label to either show a date from the  database, or if it is Null, then to say "open".

But here he says or if it is NULL

Maybe he has to change his table to have null instead of 0
0
 
ocdcAuthor Commented:
The table says "NULL".  
I tried  <asp:Label ID="End_DateLabel" runat="server" Text='<%# Eval("End_Date") == null ? "Open" : Eval("End_Date", "{0:d}")%>' />, but was not able to get it to work.
0
 
Seven priceFull StackCommented:
is this is a control for example gridview or datalist?
0
 
ocdcAuthor Commented:
FormView.  
0
 
Seven priceConnect With a Mentor Full StackCommented:
Ok you have to use the panel to allow visible or not visible and add the label inside the panel.


 <asp:FormView ID="FormView1" runat="server">
    <ItemTemplate>
    <asp:Panel ID="Panel5" runat="server" visible='<%# IIf(Eval("Total") Is DbNull.Value, "False","True") %>'>
                          <%#IIf(Format(Eval("Cflag")) & String.Empty = ("0"), (Eval("Total", "<u>Total Available</u>" & "&nbsp;<font color=red>( " & "{0}" & " )</font>")), (Eval("Total", "&nbsp;")))%>
                       
 </asp:Panel>
 </ItemTemplate>
    </asp:FormView>

0
 
lazyberezovskyConnect With a Mentor Commented:
You can move complex data binding expression to codebehind like below.
There you can easily check for DbNull and return anything you want.
<asp:Label ID="End_DateLabel" runat="server" Text='<%# GetDateString(Container.DataItem)%>' />

// Codebehind
protected string GetDateString(object dataItem)
{
    object obj = DataBinder.Eval(dataItem, "End_Date");

    if ((obj == DBNull.Value) || (obj == null))
        return "Open";

    return String.Format("{0:d}", obj);
}

Open in new window

0
 
ocdcAuthor Commented:
Sorry that I'm just gettin back to this.  I had a server that went down.
lazyberezovsky comment above did not return any errors, but also nothing showed.
0
 
ocdcAuthor Commented:
sevensnake77, I'm getting an Error Creating Control when I use that code.  I have not used Panels.  Can I use the  </asp:Panel> towards the end of the code for the Form view, like in your example?
Or should it be with the item that is being bound?
<br />
                Classification:
                <asp:TextBox ID="ClassificationTextBox" runat="server" 
                    Text='<%# Bind("Classification") %>' />
                <br />
                Internal_Only:
                <asp:TextBox ID="Internal_OnlyTextBox" runat="server" 
                    Text='<%# Bind("Internal_Only") %>' />
                <br />
 </asp:Panel>
</ItemTemplate>
  </asp:FormView>

Open in new window

0
 
Seven priceFull StackCommented:
The panel is just like an outer shell that reads the Value from the table. If the table value is null then that part is not visible if there is data the it will enable the panel to make the data visible.
0
 
ocdcAuthor Commented:
This code is producing an error: <asp:Panel ID="Panel5" runat="server" visible='<%# IIf(Eval("Total") Is DbNull.Value, "False","True") %>'>
                         <%#IIf(Format(Eval("Cflag")) & String.Empty = ("Open"), (Eval("Total", "<u>Total Available</u>" & "&nbsp;<font color=red>( " & "{0}" & " )</font>")), (Eval("Total", "&nbsp;")))%>

Error:  Compiler Error Message: CS0103: The name 'IIf' does not exist in the current context
0
 
lazyberezovskyCommented:
Because IIf is VB.NET analog of C# ?: operator.
For C# it will be:
<asp:Panel ID="Panel5" runat="server" visible='<%# (Eval("Total") == DbNull.Value) ? "False" : "True" %>'>

Open in new window

0
 
ocdcAuthor Commented:
lazyberezovsky, I'm getting an error.
Compiler Error Message: CS0103: The name 'DbNull' does not exist in the current context

Source Error:

<asp:Panel ID="Panel5" runat="server" visible='<%# (Eval("Total") == DbNull.Value) ? "False" : "True" %>'>
       
 

Open in new window

0
 
ocdcAuthor Commented:
This is easy on a grideview, but I'm not able to reproduce it on a formview.
<asp:BoundField DataField="End_Date" HeaderText="<%$ Resources:Resource, lblCloses %>" NullDisplayText="Open" DataFormatString="{0:d}"
                    SortExpression="End_Date" >

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.