2

I have a gridview in C# asp.net Web 4.5 Framework that works great until a null valued is passed for a field I am formatting as a date..

here is my template field

<asp:templatefield>
    <HeaderTemplate>
        <asp:Label ID="lblHeadEmailFirstSendDate" runat="server" Text="1st Email<br />Target Date"></asp:Label>
    </HeaderTemplate>
    <ItemTemplate>
        <asp:Label ID="lblEmailFirstSendDate" runat="server" Text='<%#  Convert.ToDateTime(Eval("EmailTargetFirstSendDate")).ToString("MM/dd/yyyy")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:Label runat="server"  ID="txtEmailFirstSendDate" Text='<%#Convert.ToDateTime(Eval("EmailTargetFirstSendDate")).ToString("MM/dd/yyyy")%>'></asp:Label>
    </EditItemTemplate>
</asp:templatefield>

I've searched high and low to find a solution that both allows me to format the date and doesn't generate an exception when the date is null.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Danimal111
  • 1,976
  • 25
  • 31

3 Answers3

4

Here you go:

<%#  Eval("EmailTargetFirstSendDate") != null ? Convert.ToDateTime(Eval("EmailTargetFirstSendDate")).ToString("MM/dd/yyyy") : "No Date" %>
MaxOvrdrv
  • 1,780
  • 17
  • 32
  • I couldn't get this to work... it was telling me there was a syntax error on page load - it was looking for a '.' I ended up using code behind to test for a null with a DateTime.TryParse() and pass back an empty string if it was null – Danimal111 May 15 '14 at 18:10
  • 1
    oh right... it was looking for a : ... i posted it wrong. sorry, i've updated my post above. and no problems for giving it to the other guy. – MaxOvrdrv May 16 '14 at 01:57
  • thanks for the update -- that's a much nicer way of going about it! Giving you credit as this is (IMHO) the best answer -- might save someone else the pain of figuring this out on their own. – Danimal111 May 16 '14 at 18:38
2

OK... I found a nice solutions (almost immediately after posting) Thanks MaxOvrdr for an answer, but I couldn't get it to work. I gave Stan credit as he nudged me in the right direction.

I added code behind:

    protected string GetDate(object  strDt)
    {
        DateTime dt1;
        if (DateTime.TryParse(strDt.ToString(), out dt1))
        {

            return dt1.ToString("MM/dd/yyyy");
        }
        else
        {
            return "";
        }


    }`

and modified the template text field to:

<asp:TemplateField > <HeaderTemplate> <asp:Label ID="lblHeadEmailFirstSendDate" runat="server" Text="1st Email<br />Target Date"></asp:Label> </HeaderTemplate> <ItemTemplate> <asp:Label ID="lblEmailFirstSendDate" runat="server" Text='<%# GetDate(Eval("EmailTargetFirstSendDate"))%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label runat="server" ID="txtEmailFirstSendDate" Text='<%# GetDate(Eval("EmailTargetFirstSendDate"))%>'></asp:Label> </EditItemTemplate> </asp:TemplateField>

And Like Magic... it works!!! Thanks to all.

Danimal111
  • 1,976
  • 25
  • 31
1

Use type DateTime? This will allow you to assign null to it

Adding the question mark turns it into a nullable type

Bind the data in the code behind? In the RowDataBound event

Standage
  • 1,517
  • 7
  • 22
  • 41
  • I bound it in the codebehind (didn't know I could do that)... see my answer below... I will give you credit as you got me going in the right direction. – Danimal111 May 15 '14 at 18:09