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

DateTimeColumn Null Exception

1 Answer 80 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Shaun asked on 02 Jul 2012, 02:52 PM
Hi I urgantly need some help
I have a RadGrid with myltiple Fields of which 2 is DateTimeColumns
I use the NeedDataSource to get data from a DataTable in the Database.
I need the EndDate Column to be able to Take nulls and Display Nulls When Returned from Database.
I can Insert Send Null Values To the Database When Inserting. But When I Need to Get The Null Date Back From The Database
I keep getting the Error: Specified cast is not valid.  regarding the DateTime of EndDate.

This is my Form Code:
<telerik:RadGrid ID="rgParameters" runat="server"
          AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"
          AlternatingItemStyle-BackColor="white" GridLines="None"
          GroupingEnabled="False" Width="100%" Height="500px"
          <AlternatingItemStyle BackColor="White" />
          <MasterTableView Caption="Parameters"
                  <telerik:GridBoundColumn AllowFiltering="False" AllowSorting="False"
                      DataField="ScheduleParaID" HeaderText="ScheduleParaID"
                      ReadOnly="true" UniqueName="ScheduleParaID" Visible="False">
                  <telerik:GridDateTimeColumn AllowFiltering="False" DataField="StartDate"
                      HeaderText="Start Date" UniqueName="StartDate" DataFormatString="{0:dd/MM/yyyy}">
                  <telerik:GridDateTimeColumn AllowFiltering="False" DataField="EndDate"
                      HeaderText="End Date" UniqueName="EndDate" DataFormatString="{0:dd/MM/yyyy}" Reorderable="True">
                  <telerik:GridNumericColumn AllowFiltering="False" DataField="WeekScheduled"
                      HeaderText="Trains Scheduled (Week Days)" UniqueName="WeekScheduled" AllowRounding="False">
                  <telerik:GridNumericColumn AllowFiltering="False" DataField="WEPHScheduled"
                      HeaderText="Train Scheduled (WE and Public Holidays)" UniqueName="WEPHScheduled"  AllowRounding="False">
              <EditFormSettings EditFormType="AutoGenerated"></EditFormSettings>

This is my Code Behind part one.
protected void rgParameters_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
            TrainOperationParameters tparam = new TrainOperationParameters();
            rgParameters.DataSource = tparam.Select();
            tparam = null;  

This is my Code Behind For My DataEntity
[DataObjectMethod(DataObjectMethodType.Select, true)]
        public List<TrainOperationParameters> Select()
            List<TrainOperationParameters> list = new List<TrainOperationParameters>();
            DataTable dt;
                dt = DataMethods.ReturnMultipleRowResultSet("[IM].[GetTrainOpParameters]", String.Empty);
                if (dt.Rows.Count > 0)
                    foreach (DataRow dr in dt.Rows)
                        TrainOperationParameters param = new TrainOperationParameters();
                        param.ScheduleParaID = Convert.ToInt32(dr["TrainOpParaID"]);
                        param.StartDate = (DateTime)dr["StartDate"];
                        param.EndDate = ((DateTime)dr["EndDate"]);
                        param.WeekScheduled = Convert.ToInt32(dr["WeekScheduled"]);
                        param.WEPHScheduled = Convert.ToInt32(dr["WEPHScheduled"]);
            catch (Exception)
                dt = null;
            return list;

Any Advice on why I keep Getting the Error and how I might be able to Fix it.

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 05 Jul 2012, 07:26 AM

You should not have problems display Null values in GridDateTimeColumn. I have made a sample project to test your case.

Give the project a try and check what are the differences between your project and this one.

the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question