This is a migrated thread and some comments may be shown as answers.

can't Bind RadDatePicker

7 Answers 330 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
mac
Top achievements
Rank 1
mac asked on 17 Jul 2008, 06:02 PM
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:                
   
 


7 Answers, 1 is accepted

Sort by
0
Kevin Babcock
Top achievements
Rank 1
answered on 18 Jul 2008, 02:36 PM
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.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />   
     
        <telerik:RadGrid ID="RadGrid1" runat="server"  
            AutoGenerateEditColumn="True"  
            AutoGenerateColumns="false" 
            AllowAutomaticUpdates="true" 
            DataSourceID="SqlDataSource1"
            <MasterTableView DataKeyNames="OrderID" DataSourceID="SqlDataSource1"
                <Columns> 
                    <telerik:GridTemplateColumn DataField="OrderID"
                        <ItemTemplate> 
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("OrderID") %>' /> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("OrderID") %>' /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridTemplateColumn DataField="ShippedDate"
                        <ItemTemplate> 
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ShippedDate") %>' /> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            Shipped:  
                            <telerik:RadDatePicker ID="RadDatePicker1" runat="server" 
                                SelectedDate='<%# Bind("ShippedDate") %>' 
                                MinDate="1/1/1950" 
                                Culture="English (United States)"
                            </telerik:RadDatePicker> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridTemplateColumn DataField="ShipName"
                        <ItemTemplate> 
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("ShipName") %>' />                          
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            Name: 
                            <telerik:RadTextBox ID="RadTextBox1" runat="server" Text='<%# Bind("ShipName") %>' /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridTemplateColumn DataField="ShipAddress"
                        <ItemTemplate> 
                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("ShipAddress") %>' /> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            Address: 
                            <telerik:RadTextBox ID="RadTextBox2" runat="server" Text='<%# Bind("ShipName") %>' /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
         
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
            ConnectionString="Data Source=.\SQLSERVER;Initial Catalog=Northwind;User ID=sa;Password=********"  
            ProviderName="System.Data.SqlClient"  
            SelectCommand="SELECT OrderID, ShippedDate, ShipName, ShipAddress FROM Orders"  
            UpdateCommand="UPDATE Orders SET ShippedDate = @ShippedDate, ShipName = @ShipName, ShipAddress = @ShipAddress WHERE (OrderID = @OrderID)"
            <UpdateParameters> 
                <asp:Parameter Name="ShippedDate" /> 
                <asp:Parameter Name="ShipName" /> 
                <asp:Parameter Name="ShipAddress" /> 
                <asp:Parameter Name="OrderID" /> 
            </UpdateParameters> 
        </asp:SqlDataSource> 
         
    </form> 
</body> 
</html> 
 

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
0
mac
Top achievements
Rank 1
answered on 18 Jul 2008, 02:46 PM
The column value is smallDateTime.
0
Daniel
Telerik team
answered on 18 Jul 2008, 03:14 PM
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
0
Kevin Babcock
Top achievements
Rank 1
answered on 18 Jul 2008, 03:15 PM
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
0
mac
Top achievements
Rank 1
answered on 21 Jul 2008, 03:51 PM
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

0
Accepted
Vlad
Telerik team
answered on 21 Jul 2008, 03:54 PM
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
0
mac
Top achievements
Rank 1
answered on 21 Jul 2008, 04:26 PM
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.
Tags
Calendar
Asked by
mac
Top achievements
Rank 1
Answers by
Kevin Babcock
Top achievements
Rank 1
mac
Top achievements
Rank 1
Daniel
Telerik team
Vlad
Telerik team
Share this question
or