ASP.NET GridView删除'输入字符串格式不正确'

时间:2015-03-15 22:30:23

标签: c# asp.net string gridview onclientclick

我有一个ASP.NET GridView,可以显示我数据库中的服装产品。我的数据库中的一个字段是Price,并将其显示为£xxx.xx我已将Price DataFormatString设置为{0:C}。

GridView还有一个删除按钮,我已将其转换为模板并添加了

OnClientClick="return confirm('Are you sure you want to delete this product?');"

单击按钮后添加确认消息。但是,当我单击确定以确认删除产品时,我收到此错误消息:

  

输入字符串的格式不正确。

当我从GridView中取出DataFormatString以便价格显示为默认的xxx.xxxx时,删除功能正常工作。只有当DataFormatString存在时才会出现此错误。我该如何解决这个问题?帮助赞赏。

HTML

<div id="GridView">
    <asp:GridView ID="ProductsGridView" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellSpacing="-1" DataKeyNames="Id" DataSourceID="ProductsGridView_DS" GridLines="None" CssClass="ProductsGrid"     AlternatingRowStyle-CssClass="alt">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select" Text="Select"></asp:LinkButton>
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this     product?');"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" DataFormatString="{0:C}" />
            <asp:CheckBoxField DataField="Available" HeaderText="Available" SortExpression="Available" />
            <asp:BoundField DataField="Stock" HeaderText="Stock" SortExpression="Stock" />
            <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="UploadImage.aspx?Id={0}" Text="Upload Image" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="ProductsGridView_DS" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:db_1318766_zaraConnectionString %>" DeleteCommand="DELETE FROM [Products] WHERE     [Id] = @original_Id AND [Name] = @original_Name AND [Description] = @original_Description AND [Gender] = @original_Gender AND [Type] = @original_Type AND [Price] = @original_Price AND (([Available] = @original_Available)     OR ([Available] IS NULL AND @original_Available IS NULL)) AND [Stock] = @original_Stock" InsertCommand="INSERT INTO [Products] ([Name], [Description], [Gender], [Type], [Price], [Available], [Stock]) VALUES (@Name,     @Description, @Gender, @Type, @Price, @Available, @Stock)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Products]" UpdateCommand="UPDATE [Products] SET [Name] = @Name, [Description] =     @Description, [Gender] = @Gender, [Type] = @Type, [Price] = @Price, [Available] = @Available, [Stock] = @Stock WHERE [Id] = @original_Id AND [Name] = @original_Name AND [Description] = @original_Description AND [Gender]     = @original_Gender AND [Type] = @original_Type AND [Price] = @original_Price AND (([Available] = @original_Available) OR ([Available] IS NULL AND @original_Available IS NULL)) AND [Stock] = @original_Stock">
        <DeleteParameters>
            <asp:Parameter Name="original_Id" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_Description" Type="String" />
            <asp:Parameter Name="original_Gender" Type="String" />
            <asp:Parameter Name="original_Type" Type="String" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
            <asp:Parameter Name="original_Available" Type="Boolean" />
            <asp:Parameter Name="original_Stock" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Gender" Type="String" />
            <asp:Parameter Name="Type" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="Available" Type="Boolean" />
            <asp:Parameter Name="Stock" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Gender" Type="String" />
            <asp:Parameter Name="Type" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="Available" Type="Boolean" />
            <asp:Parameter Name="Stock" Type="Int32" />
            <asp:Parameter Name="original_Id" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_Description" Type="String" />
            <asp:Parameter Name="original_Gender" Type="String" />
            <asp:Parameter Name="original_Type" Type="String" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
            <asp:Parameter Name="original_Available" Type="Boolean" />
            <asp:Parameter Name="original_Stock" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
</div>

1 个答案:

答案 0 :(得分:1)

尝试简化删除声明吗?

示例:

DELETE FROM [Products] WHERE [Id] = @original_Id

我说这是因为我看到你删除声明要求:“AND [Price] = @original_Price”。您的价格格式可能会影响您删除语句。

因此当你删除它(价格格式)。一切似乎再次正常。