ASP.NET - 导致这个强制转换错误的原因

时间:2013-08-07 20:57:06

标签: asp.net casting ysod

拥有一个asp.net应用程序,我正在为详细信息视图编写插入,更新和删除操作

我的插件效果很好,现在我正在尝试进行更新,但是当我按下更新按钮时,我收到了YSOD。表单上只有一个日期,并且使用完全相同的模板进行插入和编辑。下面是asp和堆栈跟踪,从我可以看到它现在不在我的任何潜艇中:

<asp:DetailsView ID="dvInvoice" runat="server" Height="50px" Width="250px" DataSourceID="odsInvoices" AutoGenerateRows="False" DataKeyNames="ID">
        <Fields>
            <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TripNo" SortExpression="TripNo">
                <EditItemTemplate>
                    <asp:TextBox ID="txtTripNo" runat="server" Text='<%# Bind("TripNo") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="txtTripNo" runat="server" Text='<%# Bind("TripNo") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ValidationGroup="vDetailErrors"
                        CssClass="ErrorClass" ID="rfvTripNo" runat="server"
                        ErrorMessage="An trip no must entered"
                        ControlToValidate="txtTripNo"
                        Display="Dynamic">
                    </asp:RequiredFieldValidator>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("TripNo") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TypeID" SortExpression="TypeID">
                                    <InsertItemTemplate>
                    <asp:DropDownList ID="ddLineTypes"
                        runat="server"
                        SelectedValue='<%# Bind("TypeID")%>'
                        DataSourceID="odsLineTypes"
                        DataTextField="TypeDesc"
                        DataValueField="TypeID"
                        AppendDataBoundItems="True" AutoPostBack="True" OnSelectedIndexChanged="Type_Change"
                         >
                        <asp:ListItem Value="-1" Selected="True">Select a Type</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:CompareValidator ID="cvLineTypes" runat="server"
                        ControlToValidate="ddLineTypes" CssClass="ErrorClass"
                        ErrorMessage="Please Select a Type"
                        ValidationGroup="vDetailErrors" ValueToCompare="-1" Operator="NotEqual"
                        Display="Dynamic"></asp:CompareValidator>
                    <asp:ObjectDataSource ID="odsLineTypes" runat="server" OldValuesParameterFormatString="original_{0}" TypeName="LineTypesBLL" SelectMethod="GetLineTypes">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="True" Name="InvOnly" Type="Boolean" />
                        </SelectParameters>
                    </asp:ObjectDataSource>
                </InsertItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddLineTypes"
                        runat="server"
                        SelectedValue='<%# Session("CurrType")%>'
                        DataSourceID="odsLineTypes"
                        DataTextField="TypeDesc"
                        DataValueField="TypeID"
                        AppendDataBoundItems="True" AutoPostBack="True" OnSelectedIndexChanged="Type_Change"
                         >
                    </asp:DropDownList>
                    <br />
                    <asp:CompareValidator ID="cvLineTypes" runat="server"
                        ControlToValidate="ddLineTypes" CssClass="ErrorClass"
                        ErrorMessage="Please Select a Type"
                        ValidationGroup="vDetailErrors" ValueToCompare="-1" Operator="NotEqual"
                        Display="Dynamic"></asp:CompareValidator>
                    <asp:ObjectDataSource ID="odsLineTypes" runat="server" OldValuesParameterFormatString="original_{0}" TypeName="LineTypesBLL" SelectMethod="GetLineTypes">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="True" Name="InvOnly" Type="Boolean" />
                        </SelectParameters>
                    </asp:ObjectDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("TypeID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="VendorID" SortExpression="VendorID">
                                    <EditItemTemplate>
                    <asp:DropDownList ID="ddVendors"
                        runat="server"
                        SelectedValue='<%# Bind("VendorID")%>'
                        DataSourceID="odsVendors"
                        DataTextField="Name"
                        DataValueField="VendorID"
                        AppendDataBoundItems="True"
                        AutoPostBack ="True">
                        <asp:ListItem Value="-1">Select a Vendor</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:CompareValidator ID="cvVendors" runat="server"
                        ControlToValidate="ddVendors" CssClass="ErrorClass"
                        ErrorMessage="Please Select a Vendor"
                        ValidationGroup="vDetailErrors" ValueToCompare="-1" Operator="NotEqual"
                        Display="Dynamic"></asp:CompareValidator>
                    <asp:ObjectDataSource ID="odsVendors" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetVendors" TypeName="VendorsBLL"></asp:ObjectDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("VendorID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="InvNo" SortExpression="InvNo">
                                  <EditItemTemplate>
                    <asp:TextBox ID="txtInvNo" runat="server" Text='<%# Bind("InvNo") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ValidationGroup="vDetailErrors"
                        CssClass="ErrorClass" ID="rfvInvNo" runat="server"
                        ErrorMessage="An invoice number must be entered"
                        ControlToValidate="txtInvNo"
                        Display="Dynamic">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("InvNo") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="InvAmount" SortExpression="InvAmount">
                                   <EditItemTemplate>
                    <asp:TextBox ID="txtInvAmount" runat="server" Text='<%# Bind("InvAmount") %>'></asp:TextBox>
                    <br />
                    <asp:RequiredFieldValidator ValidationGroup="vDetailErrors"
                        CssClass="ErrorClass" ID="rfvInvAmount" runat="server"
                        ErrorMessage="An invoice amount must be entered"
                        ControlToValidate="txtInvAmount"
                        Display="Dynamic"></asp:RequiredFieldValidator>
                    <br />
                    <asp:CompareValidator ValidationGroup="vDetailErrors"
                        CssClass="ErrorClass" ID="cvInvAmount" runat="server"
                        ControlToValidate="txtInvAmount"
                        ErrorMessage="A numeric Invoice Amount MUST be entered"
                        Operator="DataTypeCheck" Type="Currency"
                        Display="Dynamic"></asp:CompareValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("InvAmount") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="InvDate" SortExpression="InvDate">
                                   <EditItemTemplate>
                    <asp:TextBox ID="txtInvDate" runat="server" Text='<%# Bind("InvDate") %>'></asp:TextBox>
                    <br />
                    <asp:RequiredFieldValidator ValidationGroup="vDetailErrors"
                        CssClass="ErrorClass" ID="rfvInvDate" runat="server"
                        ErrorMessage="An invoice date must be entered"
                        ControlToValidate="txtInvDate"
                        Display="Dynamic">
                    </asp:RequiredFieldValidator>
                    <br />
                    <asp:CustomValidator ValidationGroup="vDetailErrors"
                        CssClass="ErrorClass"
                        ID="cvInvDate" runat="server"
                        ErrorMessage="A valid date within the last two years and no later than next year must be entered"
                        OnServerValidate="cvInvDate_ServerValidate" ControlToValidate="txtInvDate"
                        Display="Dynamic">
                    </asp:CustomValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("InvDate") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField CausesValidation="true" ShowEditButton="true" ShowInsertButton="true" ValidationGroup="vDetailErrors" />
        </Fields>
    </asp:DetailsView>
    <p>
        <asp:ObjectDataSource ID="odsInvoices" runat="server" InsertMethod="AddInvoice" OldValuesParameterFormatString="original_{0}" SelectMethod="GetInvoicesByTripNo" TypeName="InvoicesBLL" UpdateMethod="UpdateInvoice">
            <InsertParameters>
                <asp:ControlParameter ControlID="dvInvoice" Name="TripNo" PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="dvInvoice" Name="TypeID" PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="dvInvoice" Name="VendorID" PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="dvInvoice" Name="InvNo" PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="dvInvoice" Name="InvAmount" PropertyName="SelectedValue" Type="Decimal" />
                <asp:ControlParameter ControlID="dvInvoice" Name="InvDate" PropertyName="SelectedValue" Type="DateTime" />
                <asp:Parameter Name="ID" Type="Int32" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="txtLoadNo" Name="TripNo" PropertyName="Text" Type="String" />
            </SelectParameters>

            <UpdateParameters>
                <asp:ControlParameter ControlID="dvInvoice" Name="TripNo" PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="dvInvoice" Name="TypeID" PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="dvInvoice" Name="VendorID" PropertyName="SelectedValue" Type="Int32" />
                <asp:ControlParameter ControlID="dvInvoice" Name="InvNo" PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="dvInvoice" Name="InvAmount" PropertyName="SelectedValue" Type="Decimal" />
                <asp:ControlParameter ControlID="dvInvoice" Name="InvDate" PropertyName="SelectedValue" Type="DateTime" />
                <asp:ControlParameter ControlID="dvInvoice" Name="original_ID" PropertyName="SelectedValue" Type="Int32" />
            </UpdateParameters>

        </asp:ObjectDataSource>

YSOD错误

从'Int32'到'DateTime'的无效演员表。   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidCastException:从'Int32'到'DateTime'的无效转换。

来源错误:

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。

堆栈追踪:

[InvalidCastException: Invalid cast from 'Int32' to 'DateTime'.]
   System.Int32.System.IConvertible.ToDateTime(IFormatProvider provider) +134
   System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +519
   System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +126
   System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
   System.Web.UI.WebControls.Parameter.get_ParameterValue() +40
   System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +247
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +1440
   System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +87
   System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg, Boolean causesValidation) +1091
   System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +425
   System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +89
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +80
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156
   System.Web.UI.WebControls.LinkButton.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) +9642338
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

1 个答案:

答案 0 :(得分:1)

您对InsertParameters和UpdateParameters的设置错误。

在objectdatasource中,您的 ControlID =“dvInvoice” PropertyName =“SelectedValue”。此选定值是DetailsView的DataKey,它是ID( DataKeyNames =“ID”)。因此,您将所有属性设置为从ID获取数据,即Int,而不是datetime。

您可能会更多地了解DetailsView.SelectedValue属性here和ControlParameter.PropertyName属性here