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

DateTimeColumn Null Exception

1 Answer 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shaun
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"
          AutoGenerateColumns="False"
          ondeletecommand="rgParameters_DeleteCommand"
          oninsertcommand="rgParameters_InsertCommand"
          onneeddatasource="rgParameters_NeedDataSource"
          onupdatecommand="rgParameters_UpdateCommand"
          oniteminserted="rgParameters_ItemInserted">
          <AlternatingItemStyle BackColor="White" />
          <MasterTableView Caption="Parameters"
              CommandItemDisplay="TopAndBottom"
              DataKeyNames="ScheduleParaID,StartDate,EndDate,WeekScheduled,WEPHScheduled"
              ShowFooter="true">
              <Columns>
                  <telerik:GridBoundColumn AllowFiltering="False" AllowSorting="False"
                      DataField="ScheduleParaID" HeaderText="ScheduleParaID"
                      ReadOnly="true" UniqueName="ScheduleParaID" Visible="False">
                  </telerik:GridBoundColumn>
                  <telerik:GridDateTimeColumn AllowFiltering="False" DataField="StartDate"
                      HeaderText="Start Date" UniqueName="StartDate" DataFormatString="{0:dd/MM/yyyy}">
                  </telerik:GridDateTimeColumn>
                  <telerik:GridDateTimeColumn AllowFiltering="False" DataField="EndDate"
                      HeaderText="End Date" UniqueName="EndDate" DataFormatString="{0:dd/MM/yyyy}" Reorderable="True">
                  </telerik:GridDateTimeColumn>
                  <telerik:GridNumericColumn AllowFiltering="False" DataField="WeekScheduled"
                      HeaderText="Trains Scheduled (Week Days)" UniqueName="WeekScheduled" AllowRounding="False">
                  </telerik:GridNumericColumn>
                  <telerik:GridNumericColumn AllowFiltering="False" DataField="WEPHScheduled"
                      HeaderText="Train Scheduled (WE and Public Holidays)" UniqueName="WEPHScheduled"  AllowRounding="False">
                  </telerik:GridNumericColumn>
              </Columns>
              <EditFormSettings EditFormType="AutoGenerated"></EditFormSettings>
          </MasterTableView>
      </telerik:RadGrid>

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;
 
            try
            {
                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"]);
                        list.Add(param);
                    }
                }
            }
            catch (Exception)
            {
 
                throw;
            }
            finally
            {
                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
0
Andrey
Telerik team
answered on 05 Jul 2012, 07:26 AM
Hi,

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.

Greetings,
Andrey
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.
Tags
Grid
Asked by
Shaun
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or