1

Have an asp.net app and am writing insert, update and delete operations for a details view

My insert works great, now I'm trying to do the update, but I'm getting YSOD when I press the update button. There's only one date on the form and that uses exactly same template for insert and edit. Below is the asp and stack trace and from what I can see it's not in any of my subs at this point:

<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 Error

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) +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
mark1234
  • 1,110
  • 2
  • 24
  • 41
  • So is `InvDate` coming back as integer incorrectly from the database, is it selecting the wrong column? – Karl Anderson Aug 07 '13 at 20:59
  • Why are you passing dvInvoice multiple times in the Update/Insert parameters? You were using one control to pass several value for different data type, that seems weird – codingbiz Aug 07 '13 at 21:07
  • @Karl, a bit puzzled about where to look, it's same datasource for both insert and update and insert is working. When step through code, it gets past the custom validator for the date (runs through this validator sub 3 times for some unknown reason), then it comes up with the YSOD. It's not even getting to my BLL where the update method is. Would something else be reading from DB? – mark1234 Aug 07 '13 at 22:02
  • @codingbiz are you referring to the asp:ControlParameter ControlID="dvInvoice" bit when you say, why am I passing its multiple times. It a details view (dvInvoices) and the names are the controls in the DV. That was all auto-generated code. What should I change? – mark1234 Aug 07 '13 at 22:04
  • From what I can infer, you are passing the same value to all parameters of `UpdateInvoice`. That's what it means. Each parameter should have a control they map to. I don't think the value of `dvInvoice` can be Integer, String, DateTime etc at the same time.... ??? So converting a string value (`SelectedValue`) to Int32, DateTime etc could be why you were getting the error. – codingbiz Aug 07 '13 at 22:09
  • @codingbiz - thanks. that was it. no idea how I got all that garbage in there. In the end I had to use ControlID="dvInvoice$txtInvoNo" etc. Never see that syntax, but got cannot find control if I didn't use that – mark1234 Aug 08 '13 at 15:37

1 Answers1

1

Your settings for InsertParameters and UpdateParameters are wrong.

In the objectdatasource, your ControlID="dvInvoice" and PropertyName="SelectedValue". This selected value is the DataKey of the DetailsView, which is ID ( DataKeyNames="ID"). So, you have all properties set to get data from ID, which is Int, not datetime.

You may look more into DetailsView.SelectedValue Property here and ControlParameter.PropertyName Property here.

afzalulh
  • 7,925
  • 2
  • 26
  • 37