can't Bind RadDatePicker

8 posts, 1 answers
  1. mac
    mac avatar
    137 posts
    Member since:
    May 2008

    Posted 17 Jul 2008 Link to this post

    working in an EditFormTemplate within a radgrid. I am trying to bind the SelectedDate.
     <td style="width: 8px; height: 42px;">  
                <telerik:RadDatePicker    SelectedDate='<%# Bind("DOB") %>' 
                 ID="tbBdate" runat="server" Culture="English (United States)" MinDate="1920-01-01">  
                    
                </telerik:RadDatePicker> 
            </td> 
     
     

    I have also tried SelectedDate='<%# DataBinder.Eval(Container.DataItem,"DOB") %>'

    I seem to always be having problems with this ctrl.

    thanks
    Mac

    this is my current error
    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 97:             DOB (if avail)</td> 
    Line 98:         <td style="width: 8px; height: 42px;">  
    Line 99:             <telerik:RadDatePicker    SelectedDate='<%# Bind("DOB") %>' 
    Line 100:             ID="tbBdate" runat="server" Culture="English (United States)" MinDate="1920-01-01">  
    Line 101:                
       
     


  2. Kevin Babcock
    Kevin Babcock avatar
    189 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2008 Link to this post

    Hello Mac,

    What is the DOB stored as in your data source? If you are storing it as something other than a datetime type then you will definitely get the exception. I was not able to reproduce the problem when testing the RadDatePicker in a RadGrid. Here is a sample I produced which uses the Northwind database.

    1 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
    2  
    3 <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    4  
    5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    6  
    7 <html xmlns="http://www.w3.org/1999/xhtml"
    8 <head runat="server"
    9     <title>Untitled Page</title> 
    10 </head> 
    11 <body> 
    12     <form id="form1" runat="server"
    13         <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />   
    14      
    15         <telerik:RadGrid ID="RadGrid1" runat="server"  
    16             AutoGenerateEditColumn="True"  
    17             AutoGenerateColumns="false" 
    18             AllowAutomaticUpdates="true" 
    19             DataSourceID="SqlDataSource1"
    20             <MasterTableView DataKeyNames="OrderID" DataSourceID="SqlDataSource1"
    21                 <Columns> 
    22                     <telerik:GridTemplateColumn DataField="OrderID"
    23                         <ItemTemplate> 
    24                             <asp:Label ID="Label1" runat="server" Text='<%# Bind("OrderID") %>' /> 
    25                         </ItemTemplate> 
    26                         <EditItemTemplate> 
    27                             <asp:Label ID="Label1" runat="server" Text='<%# Bind("OrderID") %>' /> 
    28                         </EditItemTemplate> 
    29                     </telerik:GridTemplateColumn> 
    30                     <telerik:GridTemplateColumn DataField="ShippedDate"
    31                         <ItemTemplate> 
    32                             <asp:Label ID="Label2" runat="server" Text='<%# Bind("ShippedDate") %>' /> 
    33                         </ItemTemplate> 
    34                         <EditItemTemplate> 
    35                             Shipped:  
    36                             <telerik:RadDatePicker ID="RadDatePicker1" runat="server" 
    37                                 SelectedDate='<%# Bind("ShippedDate") %>' 
    38                                 MinDate="1/1/1950" 
    39                                 Culture="English (United States)"
    40                             </telerik:RadDatePicker> 
    41                         </EditItemTemplate> 
    42                     </telerik:GridTemplateColumn> 
    43                     <telerik:GridTemplateColumn DataField="ShipName"
    44                         <ItemTemplate> 
    45                             <asp:Label ID="Label3" runat="server" Text='<%# Bind("ShipName") %>' />                          
    46                         </ItemTemplate> 
    47                         <EditItemTemplate> 
    48                             Name: 
    49                             <telerik:RadTextBox ID="RadTextBox1" runat="server" Text='<%# Bind("ShipName") %>' /> 
    50                         </EditItemTemplate> 
    51                     </telerik:GridTemplateColumn> 
    52                     <telerik:GridTemplateColumn DataField="ShipAddress"
    53                         <ItemTemplate> 
    54                             <asp:Label ID="Label4" runat="server" Text='<%# Bind("ShipAddress") %>' /> 
    55                         </ItemTemplate> 
    56                         <EditItemTemplate> 
    57                             Address: 
    58                             <telerik:RadTextBox ID="RadTextBox2" runat="server" Text='<%# Bind("ShipName") %>' /> 
    59                         </EditItemTemplate> 
    60                     </telerik:GridTemplateColumn> 
    61                 </Columns> 
    62             </MasterTableView> 
    63         </telerik:RadGrid> 
    64          
    65         <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
    66             ConnectionString="Data Source=.\SQLSERVER;Initial Catalog=Northwind;User ID=sa;Password=********"  
    67             ProviderName="System.Data.SqlClient"  
    68             SelectCommand="SELECT OrderID, ShippedDate, ShipName, ShipAddress FROM Orders"  
    69             UpdateCommand="UPDATE Orders SET ShippedDate = @ShippedDate, ShipName = @ShipName, ShipAddress = @ShipAddress WHERE (OrderID = @OrderID)"
    70             <UpdateParameters> 
    71                 <asp:Parameter Name="ShippedDate" /> 
    72                 <asp:Parameter Name="ShipName" /> 
    73                 <asp:Parameter Name="ShipAddress" /> 
    74                 <asp:Parameter Name="OrderID" /> 
    75             </UpdateParameters> 
    76         </asp:SqlDataSource> 
    77          
    78     </form> 
    79 </body> 
    80 </html> 
    81  

    I hope this is helpful. If you continue to have problems or if my example did not accurately demonstrate the functionality you are trying to implement, please let me know.

    Sincerely,
    Kevin Babcock
  3. mac
    mac avatar
    137 posts
    Member since:
    May 2008

    Posted 18 Jul 2008 Link to this post

    The column value is smallDateTime.
  4. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 18 Jul 2008 Link to this post

    Hi Mac,

    Probably you are trying to bind non-DateTime value to RadDatePicker as supposed by Kevin. The error message is a pretty descriptive itself.

    Best regards,
    Daniel
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Kevin Babcock
    Kevin Babcock avatar
    189 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2008 Link to this post

    Hello Mac,

    Using a SmallDateTime format doesn't effect the example above either. Would you mind posting a more complete snippet of your code? It might help me figure out why you are having this problem if I can duplicate the error.

    Thanks,
    Kevin Babcock
  6. mac
    mac avatar
    137 posts
    Member since:
    May 2008

    Posted 21 Jul 2008 Link to this post

    Thanks for looking at this

    FORM Tempte


     ...  
       </Columns> 
               <EditFormSettings EditFormType="Template" CaptionFormatString="Please Add Your New Employee" ColumnNumber="3" > 
           
            <FormTemplate> 
            <table style="width: 480px">  
        <tr> 
            <td colspan="3">  
                Insert New Employee Information:</td> 
            <td colspan="1">  
            </td> 
            <td colspan="1" style="width: 7px">  
            </td> 
            <td colspan="1" style="width: 7px">  
            </td> 
        </tr> 
        <tr> 
            <td style="height: 12px" nowrap="noWrap">  
                First Name</td> 
            <td style="width: 8px; height: 12px;">  
                <asp:TextBox ID="tbFname" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox></td>  
            <td style="height: 12px" nowrap="noWrap">  
                Last Name</td> 
            <td style="height: 12px">  
                <asp:TextBox ID="tbLname" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox></td>  
            <td style="width: 7px; height: 12px;" nowrap="noWrap">  
                Email:</td> 
            <td style="width: 7px; height: 12px;">  
                <asp:TextBox ID="tbEmail" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox></td>  
        </tr> 
        <tr> 
            <td nowrap="noWrap" style="height: 19px">  
                DOB (if avail)</td> 
            <td style="width: 8px; height: 19px;">  
                <telerik:RadDatePicker ID="RadDatePicker1" SelectedDate='<%# Bind("DOB") %>' runat="server">  
                </telerik:RadDatePicker> 
            </td> 
            <td nowrap="noWrap" style="height: 19px">  
                Telephone:</td> 
            <td style="height: 19px">  
                <telerik:RadMaskedTextBox ID="tbTel" runat="server" Mask="(###) ###-####" InvalidStyleDuration="100" Text='<%# Bind("Telephone") %>' TextWithLiterals="() -" Width="125px">  
                </telerik:RadMaskedTextBox></td>  
            <td style="width: 7px; height: 19px;" nowrap="noWrap">  
                SIN</td> 
            <td style="width: 7px; height: 19px;">  
                <telerik:RadMaskedTextBox ID="tbSin" runat="server" Mask="###-###-###" InvalidStyleDuration="100" Text='<%# Bind("SIN") %>' TextWithLiterals="--" Width="125px">  
                </telerik:RadMaskedTextBox></td>  
        </tr> 
        <tr> 
            <td nowrap="noWrap" style="height: 9px">  
            </td> 
            <td style="width: 8px; height: 9px;">  
            </td> 
            <td nowrap="noWrap" style="height: 9px">  
            </td> 
            <td style="height: 9px">  
            </td> 
            <td style="width: 7px; height: 9px;" nowrap="noWrap">  
            </td> 
            <td style="width: 7px; height: 9px;">  
            </td> 
        </tr> 
        <tr> 
            <td nowrap="noWrap">  
                Street</td> 
            <td style="width: 8px">  
                <asp:TextBox ID="tbStreet" runat="server" Text='<%# Bind("Streeet") %>'></asp:TextBox></td>  
            <td nowrap="noWrap">  
                City</td> 
            <td> 
                <asp:TextBox ID="TBCity" runat="server" Text='<%# Bind("City") %>'></asp:TextBox></td>  
            <td style="width: 7px" nowrap="noWrap">  
            </td> 
            <td style="width: 7px">  
            </td> 
        </tr> 
        <tr> 
            <td nowrap="noWrap">  
                Postal</td> 
            <td style="width: 8px">  
                <asp:TextBox ID="tbPostal" runat="server" Text='<%# Bind("Postal") %>'></asp:TextBox></td>  
            <td nowrap="noWrap">  
                Province</td> 
            <td> 
                <asp:DropDownList ID="DDProvince" runat="server"    
                DataSourceID="XmlDataSource1"   
                DataTextField="text" SelectedValue ='<%# Bind("Province") %>'   
                DataValueField="value" > 
                <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem> 
     
                </asp:DropDownList></td>  
            <td style="width: 7px" nowrap="noWrap">  
            </td> 
            <td style="width: 7px">  
        </tr> 
         <tr> 
                                    <td align="right" colspan="2">  
                                        <asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' 
                                            runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'>  
                                        </asp:Button>&nbsp;  
    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
    CommandName="Cancel"></asp:Button></td>  
                                </tr> 
     
    </table> 
     
                   </FormTemplate> 

    SQL table
    CREATE TABLE [ApplicantProfile] (  
        [KeyID] [int] IDENTITY (1, 1) NOT NULL ,  
        [FirstName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [LastName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [Telephone] [char] (13) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [Email] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [DOB] [smalldatetime] NULL ,  
        [SIN] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [Streeet] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [City] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [Province] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [Postal] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,  
        [TS] [datetime] NOT NULL CONSTRAINT [DF_ApplicantProfile_TS] DEFAULT (getdate()),  
        CONSTRAINT [PK_ApplicantProfile] PRIMARY KEY  CLUSTERED   
        (  
            [KeyID]  
        )  ON [PRIMARY]   
    ON [PRIMARY]  
    GO 


    DataSource
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
    ConnectionString="<%$ ConnectionStrings:DevConnectionString %>"   
    InsertCommand="INSERT INTO [ApplicantProfile] ([FirstName], [LastName], [Telephone],   
    [Email], [DOB], [SIN], [Streeet], [City], [Province], [Postal])   
    VALUES (@FirstName, @LastName, @Telephone, @Email, @DOB, @SIN, @Streeet, @City, @Province, @Postal)"   
    SelectCommand="SELECT * FROM [ApplicantProfile] WHERE ([LastName] = @LastName)" UpdateCommand="UPDATE [ApplicantProfile] SET [FirstName] = @FirstName, [LastName] = @LastName, [Telephone] = @Telephone, [Email] = @Email, [DOB] = @DOB, [SIN] = @SIN, [Streeet] = @Streeet, [City] = @City, [Province] = @Province, [Postal] = @Postal WHERE [KeyID] = @KeyID">  
        <SelectParameters> 
            <asp:ControlParameter ControlID="RadTextBox1" DefaultValue="mcdell" Name="LastName" 
                PropertyName="Text" Type="String" /> 
        </SelectParameters> 
        <InsertParameters> 
            <asp:Parameter Name="FirstName" Type="String" /> 
            <asp:Parameter Name="LastName" Type="String" /> 
            <asp:Parameter Name="Telephone" Type="String" /> 
            <asp:Parameter Name="Email" Type="String" /> 
            <asp:Parameter Name="DOB" Type="DateTime" /> 
            <asp:Parameter Name="SIN" Type="String" /> 
            <asp:Parameter Name="Streeet" Type="String" /> 
            <asp:Parameter Name="City" Type="String" /> 
            <asp:Parameter Name="Province" Type="String" /> 
            <asp:Parameter Name="Postal" Type="String" /> 
        </InsertParameters> 
    </asp:SqlDataSource> 
     

    Current Error

    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 124:            DOB (if avail)</td>
    Line 125:        <td style="width: 8px; height: 19px;">
    Line 126: <telerik:RadDatePicker ID="RadDatePicker1" SelectedDate='<%# Bind("DOB") %>' runat="server">Line 127:            </telerik:RadDatePicker>
    Line 128:        </td>

    Source File: c:\websites\hrapps\Forms\comps\CreateApplicant.ascx    Line: 126

  7. Answer
    Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Jul 2008 Link to this post

    Hello mac,

    Please try DbSelectedDate instead. Here is an example:

    <telerik:RadDatePicker ID="RadDatePicker1" DbSelectedDate='<%# Bind("DOB") %>' runat="server" />

    Best wishes,
    Vlad
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  8. mac
    mac avatar
    137 posts
    Member since:
    May 2008

    Posted 21 Jul 2008 Link to this post

    Thanks that solved it. For some reason, dbselectedDate does not come in on the intellisense and from the old posts regarding this same issue, it seemed to be no longer supported in this version of radcontrols.
    Thanks though! This saves the remainder of my hair from being ripped out.
Back to Top