troubleshooting Question

Need help with invalid cast from Int32 to Datetime

Avatar of gggoddard
gggoddard asked on
ASP.NET
4 Comments1 Solution1293 ViewsLast Modified:
Hi;

I changed something in my code and now my update procedure won't work in a datagridview Update routine.

Please see code :

Thanks for any insights
----Source from Web Page-----------------
 
<div id="grd1" style="font-family:Verdana; font-size:small; ">
      <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
          AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="ObjectDataSource4"
          EmptyDataText="There were no records found" Font-Size="Small" Style="font-size: 8pt;
          font-family: Verdana">
            <Columns>
                <asp:CommandField ShowEditButton="True" ButtonType="Button" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="ID" Visible="False" />
                <asp:TemplateField HeaderText="Assignee" SortExpression="assignee">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataSourceID="ObjectDataSource5" DataTextField="LName" DataValueField="assignee_ID" SelectedValue='<%# Bind("assignee", "{0}") %>' AppendDataBoundItems="True">
                            <asp:ListItem Value="0">Please Select</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("LName", "{0}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Assignor" SortExpression="assignor">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ObjectDataSourceDDL_Assignor"
                            DataTextField="Last" DataValueField="assignor_ID" SelectedValue='<%# Bind("assignor", "{0}") %>' AppendDataBoundItems="True" >
                            <asp:ListItem Value="0">Please Select</asp:ListItem>
                        </asp:DropDownList><br />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Last", "{0}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="requestor" HeaderText="Requestor" SortExpression="requestor"/>
                <asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
                <asp:BoundField DataField="short_name" HeaderText="Tasks" SortExpression="short_name" />
                <asp:TemplateField HeaderText="Due" SortExpression="due_date">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("due_date", "{0:d}") %>'></asp:TextBox>&nbsp;<ajaxToolkit:CalendarExtender
                            ID="CalendarExtender1" runat="server" TargetControlID="TextBox1">
                        </ajaxToolkit:CalendarExtender>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("due_date", "{0:d}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="estimated_hours" HeaderText="EST Hrs" SortExpression="estimated_hrs" />
                <asp:TemplateField HeaderText="Completed" SortExpression="completed_date">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("completed_date", "{0:d}") %>'></asp:TextBox><ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="TextBox4">
                        </ajaxToolkit:CalendarExtender>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("completed_date", "{0:d}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Projects" SortExpression="project_ID">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="ObjectDataSource3"
                            DataTextField="name" DataValueField="project_ID" AppendDataBoundItems="true" SelectedValue='<%# Bind("project_ID", "{0}") %>'>
                                              <asp:ListItem Selected="True" Text="Please Select" Value="0"></asp:ListItem>
                        </asp:DropDownList>&nbsp;
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Projcts", "{0}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Milestone" SortExpression="milestones_ID">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="ObjectDataSource2"
                            DataTextField="description" DataValueField="milestone_ID" SelectedValue='<%# Bind("milestones_ID", "{0}") %>' AppendDataBoundItems="true">
                                          <asp:ListItem Selected="True" Text="Please Select" Value="0"></asp:ListItem>
                        </asp:DropDownList>&nbsp;
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("Milestnes", "{0}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="best_case_estimate" HeaderText="BestCase" SortExpression="best_case_estimate" />
                <asp:BoundField DataField="worse_case_estimate" HeaderText="WorseCase"
                    SortExpression="worse_case_estimate" />
                <asp:BoundField DataField="baseLine_hours" HeaderText="BaseLine" SortExpression="baseLine_hours" Visible="False" />
                <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
                <asp:BoundField DataField="complete_estimated" HeaderText="complete_estimated" SortExpression="complete_estimated" />
                <asp:BoundField DataField="priority" HeaderText="xpriority" SortExpression="priority" Visible="False" />
                <asp:BoundField DataField="priority_name" HeaderText="Priority" SortExpression="priority_name" />
             
            </Columns>
        </asp:GridView>
 
---------------And the paramaters for my ODS:---------------------------
------------------------------------------------------------------------------------------------------------------
  <UpdateParameters>
            <asp:ControlParameter ControlID="DetailsView1" Name="ID" PropertyName="SelectedValue"
                Type="Int32" />
            <asp:ControlParameter ControlID="DetailsView1" Name="ASSIGNEE" PropertyName="SelectedValue"
                Type="Int32" />
            <asp:ControlParameter ControlID="DetailsView1" Name="ASSIGNOR" PropertyName="SelectedValue"
                Type="Int32" />
            <asp:ControlParameter ControlID="DetailsView1" Name="REQUESTOR" PropertyName="SelectedValue"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1" Name="DESCRIPTION" PropertyName="SelectedValue"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1" Name="SHORT_NAME" PropertyName="SelectedValue"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1" Name="DUE_DATE" PropertyName="SelectedValue"
                Type="DateTime" />
            <asp:ControlParameter ControlID="DetailsView1" Name="ESTIMATED_HOURS" PropertyName="SelectedValue"
                Type="Decimal" />
            <asp:ControlParameter ControlID="DetailsView1" Name="COMPLETED_DATE" PropertyName="SelectedValue"
                Type="DateTime" />
            <asp:ControlParameter ControlID="DetailsView1" Name="PROJECT_ID" PropertyName="SelectedValue"
                Type="Int32" DefaultValue=" " />
           <asp:ControlParameter ControlID="DetailsView1" Name="MILESTONES_ID" PropertyName="SelectedValue" 
           Type="Int32"/>
           <asp:ControlParameter ControlID="DetailsView1" Name="BEST_CASE_ESTIMATE" PropertyName="SelectedValue"
                Type="Decimal" />
            <asp:ControlParameter ControlID="DetailsView1" Name="WORSE_CASE_ESTIMATE" PropertyName="SelectedValue"
                Type="Decimal" />
            <asp:ControlParameter ControlID="DetailsView1" Name="BASELINE_HOURS" PropertyName="SelectedValue"
                Type="Decimal" />
            <asp:ControlParameter ControlID="DetailsView1" Name="COMMENTS" PropertyName="SelectedValue"
                Type="String" />
            <asp:ControlParameter ControlID="DetailsView1" Name="COMPLETE_ESTIMATED" PropertyName="SelectedValue"
                Type="Decimal" />
            <asp:ControlParameter ControlID="DetailsView1" Name="PRIORITY" PropertyName="SelectedValue"
                Type="Int32" />
        </UpdateParameters>
 
--------------And the code from the stored procedure : ---------------------------------------
--------------------------------------------------------------------------------------------------------------------
ALTER PROCEDURE tasks_update
 	(@ID [int],
 	@ASSIGNEE  	[int],
	 @ASSIGNOR 	[int],
	 @REQUESTOR 	[varchar](50),
	 @DESCRIPTION 	[varchar](50),
	 @SHORT_NAME 	[varchar](50),
	 @DUE_DATE          [datetime],
	 @ESTIMATED_HOURS     [decimal](9),
	 @COMPLETED_DATE        [datetime],
	 @PROJECT_ID       [int],
	 @MILESTONES_ID    [int], 
	 @BEST_CASE_ESTIMATE [decimal](9),
     @WORSE_CASE_ESTIMATE [decimal](9),
     @BASELINE_HOURS           [decimal](9), 
     @COMMENTS                          [varchar](50),
     @COMPLETE_ESTIMATED [decimal](9),
     @PRIORITY      [int])
        
 AS
 
SET NOCOUNT	ON
 
DECLARE @intErrorCode int
Select @intErrorCode = @@error
 
begin Transaction
 
If @intErrorCode = 0
Begin
  UPDATE [Tasks] 
  
   SET 
	 [ASSIGNEE] = @ASSIGNEE,
	 [ASSIGNOR] = @ASSIGNOR,
	 [REQUESTOR] = @REQUESTOR,
	 [DESCRIPTION] =  @DESCRIPTION, 
	 [SHORT_NAME] =  @SHORT_NAME,
	 [DUE_DATE] =  @DUE_DATE,
	 [ESTIMATED_HOURS]  = @ESTIMATED_HOURS, 
	 [COMPLETED_DATE] = @COMPLETED_DATE,
	 [PROJECT_ID] = @PROJECT_ID,
	 [MILESTONES_ID] = @MILESTONES_ID, 
	 [BEST_CASE_ESTIMATE] = @BEST_CASE_ESTIMATE,
	 [WORSE_CASE_ESTIMATE]  =  @WORSE_CASE_ESTIMATE,
	 [BASELINE_HOURS] = @BASELINE_HOURS, 
	 [COMMENTS] = @COMMENTS,
	 [COMPLETE_ESTIMATED]  =  @COMPLETE_ESTIMATED,
	 [PRIORITY] = @PRIORITY 
    
     WHERE ([ID] = @ID) 
               Select @intErrorCode = @@Error,
               @ID = @@Identity 
              
   end               
 
If @intErrorCode = 0
  COMMIT TRANSACTION
Else
  ROLLBACK TRANSACTION
return @intErrorCode	
 
---------------And the some of the contents of the error message:------------------------------
--------------------------------------------------------------------------------------------------------------------
 
Invalid cast from 'Int32' to 'DateTime'. 
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: Invalid cast from 'Int32' to 'DateTime'.
 
Source Error: 
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
 
Stack Trace: 
 
 
[InvalidCastException: Invalid cast from 'Int32' to 'DateTime'.]
   System.Int32.System.IConvertible.ToDateTime(IFormatProvider provider) +156
   System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +523
   System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +141
   System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
   System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +301
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +634
   System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +92
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +907
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +704
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +166
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros