Question

I have a gridview where I keep track of deadlines. I have 2 dates herefor. A expected date (date where the deadline is supposed to be completed) and an actual date (date where the deadline was completed). Now in my DB all the expected dates have values because they are meant to finish at around the same date each month. However the actual date is blank.

This gives me following error due to the null-values: Exception Details: System.InvalidCastException: Specified cast is not valid.

Now I found something while looking on the Internet and that is to call a helper method (see the selectedDate property - I copied to same value of selectedDate into VisibleDate, because else I had the same error) However this presents me with another error which is that I can't get the newly selected value!

<asp:TemplateField HeaderText="Actual Date" SortExpression="Actual_Date">
    <EditItemTemplate>
        <asp:Calendar ID="Calendar2" runat="server" VisibleDate='<%# Bind("Actual_Date") %>' SelectedDate='<%# FixNullDate(Eval("Actual_Date")) %>'></asp:Calendar>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" 
            Text='<%# Bind("Actual_Date", "{0:d}") %>'>
        </asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Here is my code of from my templatefield for the actual date

Please help, I'm kinda lost :p

Was it helpful?

Solution

You need to check for null:

<%# string.IsNullOrEmpty(Bind("Actual_Date").ToString()) ? "0000-00-00 00:00:00.000": Bind("Actual_Date").ToString() %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top