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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Seven priceFull 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lazyberezovskyCommented:
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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 priceFull 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
lazyberezovskyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.