Problem with calendar control in DetailsVIew using ASP.NET

A

aj

Hi

My ASP page has a Detailsview with a calendar control and textbox. The
Textbox is binding a date field in the SQL table which has NULL values
for a few records. Therefore my script is giving me the following
error message

System.InvalidCastException: Conversion from type 'DBNull' to type
'Date' is not valid.

for those records and I have not be able to solve the issue. Could
someone please help.

..ASPX page

<asp:DetailsView ID="Dtv2SDCUpdate" runat="server"
DataKeyNames="company, content"
<Fields>
<asp:TemplateField HeaderText="End Date">
<EditItemTemplate>
<asp:TextBox ID="EditenddateTextBox"
runat="server"
Text='<%# Bind("end_date") %>' />
<asp:Calendar ID="EndDateCal" runat="server"
SelectedDate='<%#
Chk(Bind("end_date")) %>'
VisibleDate='<%#
Chk(Bind("end_date")) %>'>
</asp:Calendar>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblEndDate" runat="server" Text='<
%# Bind("end_date") %>' />
</ItemTemplate>

</asp:TemplateField>
</Fields>
</asp:DetailsView>

I have a function to check is the date field is NULL

..VB
Public Function ChkDate(ByVal d As Object) As DateTime
If Not TypeOf d Is DBNull Then
Return CDate(d).ToString
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top