We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Cannot BIND to a NULL Database Field

Hi Support Team,

I am using a DatePicker as an edit control in a Gridview column. Everything works fine if the database field that the DatePicker is bound to has a value. I can change and Update the field using the DataPicker.

When the database field does not have a value (NULL) I get the following error on clicking the row's "Edit" link:

Server Error in '/' Application.

Specified cast is not valid.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 275:                    <asp:TemplateField HeaderText="End Date" SortExpression="EmploymentEndDate">
Line 276:                        <EditItemTemplate>
Line 277:                            <ej:DatePicker ID="dtpEmploymentEnd2" runat="server" DateFormat="MM/dd/yyyy" Value='<%# Bind("EmploymentEndDate") %>'></ej:DatePicker>
Line 278:                            <%--<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("EmploymentEndDate", "{0:M/d/yyyy}") %>' Width="80"></asp:TextBox>--%>
Line 279:                        </EditItemTemplate>

Source File:c:\iAM_iSTAR_fromRoger\iamistar\iamistar\Searches\EditParticipantData.aspx Line:277

Stack Trace:

[InvalidCastException: Specified cast is not valid.]
   ASP.searches_editparticipantdata_aspx.__DataBinding__control193(Object sender, EventArgs e) in c:\iAM_iSTAR_fromRoger\iamistar\iamistar\Searches\EditParticipantData.aspx:277
   System.Web.UI.Control.OnDataBinding(EventArgs e) +84
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +150
   System.Web.UI.Control.DataBind() +17
   System.Web.UI.Control.DataBindChildren() +185
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +160
   System.Web.UI.Control.DataBind() +17
   System.Web.UI.Control.DataBindChildren() +185
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +160
   System.Web.UI.Control.DataBind() +17
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +167
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3660
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +67
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +34
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.WebControls.GridView.DataBind() +9
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +26
   System.Web.UI.Control.PreRenderRecursiveInternal() +88
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4775


Version Information:Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1085.0

I am using Syncfusion.EJ.Web, Version=14.1400.0.41 with .NET version 4.6 and MS SQL Server database.

In this case, I want to see an empty DatePicker with WaterMark text "Select Date".Is there a workaround for this problem? I prefer to correct this in the aspx page mark-up, but I can work in the code-behind if needed.

Thanks in advance.


3 Replies

TM Thirukumaran Murugan Syncfusion Team September 29, 2016 04:31 PM UTC

Hi Jim, 
Thanks for contacting Syncfusion support. 
We would like to let you know that the DatePicker accepts both string, Date Time object values and we have tried to reproduce the issue by creating a simple DatePicker sample with MySQL database. In this we are able to set the fetched MySQL   Date Time object value to the DatePicker as input.  
Please check with the below link resolve the typecast error “ System.InvalidCastException: Specified cast is not valid. 
Query: I get the following error on clicking the row's "Edit" link: 
Could you please explain more about this scenario or share the sample to reproduce the issue at our end? This will be helpful for us to analyze and provide a solution at the earliest. 
 
Regards, 
Thirukumaran M. 



JL Jim Lammy September 29, 2016 05:38 PM UTC

Thank you Thirukumaran,

Your fast response is appreciated.  The link you posted was very interesting. 

I was able to get this to work in code-behind, the working GridView is below.  I just hoped there would be a way to do this in the aspx mark-up (Wish List).


The "End Date" in the Update row of this record is blank in the database.  The user can then select a new "End Date" or leave it blank for the Update.

Best regards,
Jim Lammy


TM Thirukumaran Murugan Syncfusion Team September 30, 2016 01:09 PM UTC

Hi Jim, 
 
Yes, you can achieve your requirement in the aspx mark-up page itself. 
Please refer to the below code snippet. 
 
Code block: 
 
// Define language as C# within the script tag.  
<script language="c#" runat="server">              
        protected void Button1_Click(object sender, EventArgs e) 
        { }   
        //Code behind logic. 
    </script> 
 
 
 
In the above code block, we have defined the script tag with language as C#, to achieve the code behind logic within the ASPX markup page itself. 
For your convenience we have attached the sample in the below link. 
 
Sample location: 
 
Regards, 
Thirukumaran M. 


Loader.
Live Chat Icon For mobile
Up arrow icon