0

I want to check if Date is null then display a string . if not null display date.

  <asp:Label ID="Label9" runat="server" Text='<%# Convert.ToDateTime(Eval("Issue_Date")).ToShortDateString() %>' />

2 Answers 2

2

You could also solve it like this:

<asp:Label ID="Label9" runat="server" 
           Text='<%# (!String.IsNullOrEmpty(Eval("Issue_Date")) ? 
                 Convert.ToDateTime(Eval("Issue_Date")).ToShortDateString() :
                 "yourStringIfNull") %>' />
Sign up to request clarification or add additional context in comments.

Comments

2

Create a method in the code-behind:

public string DisplayDateTime(object value)
{
  if (value== null)
  {
     return "Date is null";
  }

  return Convert.ToDateTime(value).ToShortDateString();
}

then call DisplayDateTime from a page:

<asp:Label Text='<%# DisplayDateTime(Eval("Issue_Date")) %>' runat="server"></asp:Label>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.