C# System.FormatException和delete不工作

C# System.FormatException和delete不工作,c#,asp.net,sql-server-2012,C#,Asp.net,Sql Server 2012,我已经创建了一个gridview,供用户添加/编辑和删除产品。不是实际删除,而是在数据库中声明它已停止 我得到以下例外情况: mscorlib.dll中发生“System.FormatException”类型的异常 但未在用户代码中处理 我的标记如下: <%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminMaster.master" AutoEventWireup="true" CodeFile="addProduct.

我已经创建了一个gridview,供用户添加/编辑和删除产品。不是实际删除,而是在数据库中声明它已停止

我得到以下例外情况:

mscorlib.dll中发生“System.FormatException”类型的异常 但未在用户代码中处理

我的标记如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminMaster.master" AutoEventWireup="true" CodeFile="addProduct.aspx.cs" Inherits="Admin_addProduct" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="RightCol" runat="Server">
<h1>Products</h1>
<asp:Label ID="lblProdGrd" runat="server"></asp:Label>
<asp:SqlDataSource ID="lstCatSrc" runat="server" ConnectionString='<%$ ConnectionStrings:bncConn %>'  SelectCommand="SELECT CategoryName, CategoryId FROM ProductDetails.Category"></asp:SqlDataSource>
<asp:SqlDataSource ID="lstSubCatSrc" runat="server" ConnectionString='<%$ ConnectionStrings:bncConn %>' SelectCommand="SELECT ProductType, ProductTypeId FROM ProductDetails.ProductType"></asp:SqlDataSource>
<asp:SqlDataSource ID="lstImgSrc" runat="server" ConnectionString='<%$ ConnectionStrings:bncConn %>' SelectCommand="SELECT ImageName, ImageId, ImagePath FROM ProductDetails.ProductImages"></asp:SqlDataSource>

<asp:GridView
    ID="grdProds"
    runat="server"
    AllowPaging="true"
    ShowFooter="true"
    PageSize="5"
    AutoGenerateColumns="false"
    OnPageIndexChanging="grdProds_PageIndexChanging"
    OnRowCancelingEdit="grdProds_RowCancelingEdit"
    OnRowCommand="grdProds_RowCommand"
    OnRowEditing="grdProds_RowEditing"
    OnRowUpdating="grdProds_RowUpdating"
    onrowdeleting="grdProds_RowDeleting">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField AccessibleHeaderText="Product ID" FooterText="Product ID" HeaderText="Product ID">
            <ItemTemplate>
                <asp:Label ID="lblProdId" Text='<%# Eval("ProductId") %>' runat="server"></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblAdd" runat="server"></asp:Label>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField AccessibleHeaderText="Product Name" HeaderText="Product Name" FooterText="ProductName">
            <ItemTemplate>
                <asp:Label ID="lblname" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("ProductName") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddName" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product Description" AccessibleHeaderText="Product description" FooterText="Product Description">
            <ItemTemplate>
                <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("Description")%>' TextMode="MultiLine"></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddDescription" runat="server" TextMode="MultiLine"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product Price" AccessibleHeaderText="Product Price" FooterText="Product Price">
            <ItemTemplate>
                <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>' ></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtPrice" runat="server" Text='<%# Bind("Price") %>' TextMode="Number"></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddPrice" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblCat" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                 <asp:DropDownList ID="lstCatEdit" runat="server" DataSourceID="lstCatSrc" DataTextField="CategoryName" DataValueField="CategoryId" AppendDataBoundItems="True">
                    <asp:ListItem Text="--(Select a category)--" Value="NULL"></asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstCat" runat="server" DataSourceID="lstCatSrc" DataTextField="CategoryName" DataValueField="CategoryId" AppendDataBoundItems="True">
                    <asp:ListItem Text="--(Select a category)--" Value="NULL"></asp:ListItem>
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Sub-Category" AccessibleHeaderText="Sub-Category" FooterText="Sub-Category">
            <ItemTemplate>
                <asp:Label ID="lblSubCat" runat="server" Text='<%# Eval("ProductType") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="lstSubCat" runat="server" DataSourceID="lstSubCatSrc" DataTextField="ProductType" DataValueField="ProductTypeId"></asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstAddSubCat" runat="server" DataSourceID="lstSubCatSrc" DataTextField="ProductType" DataValueField="ProductTypeId"></asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product Image" AccessibleHeaderText="Product Image" FooterText="Product Image">
            <ItemTemplate>
                <asp:Image ID="imgProd" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("ImagePath") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="lstProdImg" runat="server" DataSourceID="lstImgSrc" DataTextField="ImageName" DataValueField="ImageId"></asp:DropDownList>
                <asp:Image ID="imgProdEdit" runat="server" Height="250" Width="250" />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstAddProdImg" runat="server" DataSourceID="lstImgSrc" DataTextField="ImageName" DataValueField="ImageId"></asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item in stock" AccessibleHeaderText="Item in stock" FooterText="Item in stock">
            <ItemTemplate>
                <asp:CheckBox ID="chkInStock" runat="server" Checked='<%# Eval("InStock") %>' Enabled="False" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="chkInStockEdit" runat="server" Checked='<%# Eval("InStock") %>' />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:CheckBox ID="chkInStockAdd" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Pre-Designed" AccessibleHeaderText="Pre-designed" FooterText="Pre-Designed">
            <ItemTemplate>
                <asp:CheckBox ID="chkPrePrinted" runat="server" Checked='<%# Eval("PrePrinted") %>' Enabled="False" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="chkPrePrintedEdit" runat="server" Checked='<%# Eval("PrePrinted") %>' />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:CheckBox ID="chkAddPrePrinted" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit">
            <ItemTemplate>
                <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
                <br />
                <span onclick="return confirm('Are you sure you want to declare this product Discontinued?')">
                    <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
                </span>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
                <br />
                <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAddRecord" runat="server" Text="Add" CommandName="Add"></asp:Button>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
我的存储过程:

CREATE PROCEDURE [ProductDetails].[bnc_Products] 
-- Add the parameters for the stored procedure here
@ProdId int = 0,
@Category int = 0,
@ProductType tinyint = 0, 
@Price money = 0,
@InStock bit = 1,
@ProductName varchar(50) = '',
@Image int = 0,
@Description varchar(max) = '',
@PrePrinted bit = 1,
@Discontinued bit = 0,
@Status varchar(50) = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

if (@Status = 'Display')
begin
select p.ProductId,p.ProductName,c.CategoryId,c.CategoryName,
       pt.ProductType,pt.ProductTypeId,p.Price,p.InStock,pi.ImagePath,
  pi.ImageId,pi.ImageName,p.Description,p.PrePrinted,p.Discontinued

 from ProductDetails.Products p
join ProductDetails.Category c on c.CategoryId = p.Category
join ProductDetails.ProductType pt on pt.ProductTypeId = p.ProductType
join ProductDetails.ProductImages pi on pi.ImageId = p.ImageId
where p.Discontinued = 0  
end
else if(@Status = 'Update')
begin
update Productdetails.Products
set Category = @Category, ProductType = @ProductType, 
Price = @Price, InStock = @InStock, ProductName = @ProductName, 
ImageId = @Image, Description = @Description, PrePrinted = @PrePrinted
where ProductId = @ProdId
end
else if(@Status = 'Add')
begin
insert into ProductDetails.Products
(Category,ProductType,Price,InStock,ProductName,ImageId,Description,PrePrinted,Discontinued)
values    (@Category,@ProductType,@Price,@InStock,@ProductName,@Image,@Description,@PrePrinted,@Discontinued)
end
else if(@Status = 'Delete')
begin
Update ProductDetails.Products
set Discontinued = @Discontinued
where ProductId = @ProdId
end
END
另外,当我试图删除一个产品时,我们会宣布它停产,似乎什么也没有发生,如果有人能够指出哪里出了问题,我会非常感激,因为我完全不知道发生了什么。很抱歉,如果这一切看起来有点模糊,我是这方面的新手,所以目前我完全处于停滞状态。向所有愿意提供帮助的人表示感谢。

您已经打开了以下asp:content,但尚未关闭它:

<asp:content id="Content3" contentplaceholderid="RightCol" runat="Server">

因此,在执行put和delete之后,函数将被执行。

您好,谢谢您的建议,但在我的标记上,它是存在的,只是在我复制和粘贴时,它没有完全转移到我的帖子中。这是唯一一个没有发表文章的东西,其他东西都存在。@M_Griffiths我刚刚在我的机器上创建了你的页面,并且工作正常。很明显,删除了列和连接等。哦,太奇怪了,它不在我的机器上。它显示的页面很好,我可以进入编辑产品,但当我试图将编辑保存到db时,我得到了上述异常。而且deleteDestructed目前似乎没有做任何事情,但没有给出任何错误或异常。网格会刷新,但产品仍然在那里,我看到了,所以在运行存储过程时,您会看到这一点。哪一行抛出了这个异常我现在已经解决了删除问题,我在代码中遗漏了一个参数,sp需要它做它需要的事情。
<asp:content id="Content3" contentplaceholderid="RightCol" runat="Server">