System.FormatException 删除无效

System.FormatException and delete not working

我创建了一个 gridview,供用户 add/edit删除 产品。 (实际上不是删除而是在数据库中声明它已停止)

我收到以下异常:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

我的标记如下:

<%@ 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>

我的代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Entity;
using BillyNicClothingModel;
public partial class Admin_addProduct : System.Web.UI.Page
{

private string connectionString =
   WebConfigurationManager.ConnectionStrings["bncConn"].ConnectionString;
string CatLst;



protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {

        BindGrid();
    }

}

protected void BindGrid()
{

    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("ProductDetails.bnc_Products", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);

    cmd.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));

    cmd.Parameters["@status"].Value = "Display";

    try
    {
        con.Open();

        DataSet ds = new DataSet();
        adapter.Fill(ds, "Products");
        grdProds.DataSource = ds;
        grdProds.DataBind();
    }
    catch (Exception err)
    {
        lblProdGrd.Text = err.Message;
    }
    finally
    {
        con.Close();
    }


}

protected void grdProds_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

    grdProds.PageIndex = e.NewPageIndex;

    BindGrid();

}
protected void grdProds_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{

    grdProds.EditIndex = -1;

    BindGrid();

}
protected void grdProds_RowCommand(object sender, GridViewCommandEventArgs e)
{

    if (e.CommandName.Equals("Add"))
    {



        TextBox txtName = (TextBox)grdProds.FooterRow.FindControl("txtAddname");
        TextBox txtDescription = (TextBox)grdProds.FooterRow.FindControl("txtAddDescription");
        TextBox txtPrice = (TextBox)grdProds.FooterRow.FindControl("txtAddPrice");
        DropDownList lstCatEdit = (DropDownList)grdProds.FooterRow.FindControl("lstCat");
        DropDownList lstSubCat = (DropDownList)grdProds.FooterRow.FindControl("lstAddSubCat");
        DropDownList lstImageProd = (DropDownList)grdProds.FooterRow.FindControl("lstAddProdImg");
        CheckBox chkInStockEdit = (CheckBox)grdProds.FooterRow.FindControl("chkInStockAdd");
        CheckBox chkPrePrinted = (CheckBox)grdProds.FooterRow.FindControl("chkAddPrePinted");

        string ProductName, Description;
        int Category, Image;
        bool InStock, Preprinted;
        decimal Price;
        byte SubCat;

        ProductName = txtName.Text;
        Description = txtDescription.Text;
        Price = Decimal.Parse(txtPrice.Text);
        Category = int.Parse(lstCatEdit.SelectedValue);
        SubCat = byte.Parse(lstSubCat.SelectedValue);
        Image = int.Parse(lstImageProd.SelectedValue);
        InStock = chkInStockEdit.Checked;
        Preprinted = chkPrePrinted.Checked;

        AddProduct(ProductName, Description, Price, Category, SubCat, Image, InStock, Preprinted);

        grdProds.EditIndex = -1;

        BindGrid();

    }

}
protected void AddProduct(string ProductName, string Description, decimal Price, int Category, int SubCat, 
    int Image, bool InStock, bool Preprinted)
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("ProductDetails.bnc_Products", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);

    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@status"].Value = "Add";
    cmd.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.VarChar, 50));
    cmd.Parameters["@ProductName"].Value = ProductName;
    cmd.Parameters.Add(new SqlParameter("@Description", Description));
    cmd.Parameters["@Description"].Value = Description;
    cmd.Parameters.Add(new SqlParameter("@Price", SqlDbType.Money));
    cmd.Parameters["@Price"].Value = Price;
    cmd.Parameters.Add(new SqlParameter("@Category", SqlDbType.Int));
    cmd.Parameters["@Category"].Value = Category;
    cmd.Parameters.Add(new SqlParameter("@ProductType", SqlDbType.TinyInt));
    cmd.Parameters["@ProductType"].Value = SubCat;
    cmd.Parameters.Add(new SqlParameter("@Image", SqlDbType.Int));
    cmd.Parameters["@Image"].Value = Image;
    cmd.Parameters.Add(new SqlParameter("@InStock", SqlDbType.Bit));
    cmd.Parameters["@InStock"].Value = InStock;
    cmd.Parameters.Add(new SqlParameter("@PrePrinted", SqlDbType.Bit));
    cmd.Parameters["@PrePrinted"].Value = Preprinted;


    try {
        con.Open();

        DataSet ds = new DataSet();
        adapter.Fill(ds);
    }
    catch (Exception err)
    {
        lblProdGrd.Text = err.Message;
    }
    finally
    {
        con.Close();
    }
}
protected void grdProds_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    Label prId = (Label)grdProds.Rows[e.RowIndex].FindControl("lblProdId");
    int pId = Convert.ToInt32(prId.Text);
    DeleteProduct(pId);
    grdProds.EditIndex = -1;

    BindGrid();

}
protected void DeleteProduct(int pId)
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("ProductDetails.bnc_Products", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);

    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@status"].Value = "Delete";
    cmd.Parameters.Add(new SqlParameter("@ProdId", SqlDbType.Int));
    cmd.Parameters["@ProdId"].Value = pId;

    try
    {
        con.Open();

        DataSet ds = new DataSet();
        adapter.Fill(ds);
    }
    catch (Exception err)
    {
        lblProdGrd.Text += err.Message;
    }
    finally
    {
        con.Close();
    }
}
protected void grdProds_RowEditing(object sender, GridViewEditEventArgs e)
{

    grdProds.EditIndex = e.NewEditIndex;

    BindGrid();

}
protected void grdProds_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    Label prdId = (Label)grdProds.Rows[e.RowIndex].FindControl("lblProdId");
    TextBox Name = (TextBox)grdProds.Rows[e.RowIndex].FindControl("txtName");
    TextBox Description = (TextBox)grdProds.Rows[e.RowIndex].FindControl("txtDescription");
    TextBox Price = (TextBox)grdProds.Rows[e.RowIndex].FindControl("txtPrice");
    DropDownList Category = (DropDownList)grdProds.Rows[e.RowIndex].FindControl("lstCatEdit");
    DropDownList SubCat = (DropDownList)grdProds.Rows[e.RowIndex].FindControl("lstSubCat");
    DropDownList Image = (DropDownList)grdProds.Rows[e.RowIndex].FindControl("lstProdImg");
    CheckBox InStock = (CheckBox)grdProds.Rows[e.RowIndex].FindControl("chkInStockEdit");
    CheckBox PrePrinted = (CheckBox)grdProds.Rows[e.RowIndex].FindControl("chkPrePrintedEdit");

    int ProdId = Convert.ToInt32(prdId.Text);
    string eName = Name.Text;
    string eDescription = Description.Text;
    Decimal ePrice = Convert.ToDecimal(Price.Text);
    int eCat = Convert.ToInt32(Category.SelectedValue);
    byte eSCat = Convert.ToByte(SubCat.SelectedValue);
    int eImage = Convert.ToInt32(Image.SelectedValue);
    bool eInstock = InStock.Checked;
    bool ePreprinted = PrePrinted.Checked;


    UpdateProduct(ProdId, eName, eDescription, ePrice, eCat, eSCat, eImage, eInstock, ePreprinted);
    grdProds.EditIndex = -1;
    BindGrid();

}

protected void UpdateProduct(int ProdId, string eName, string eDescription, Decimal ePrice, int eCat, byte eSCat, int eImage, bool eInstock, bool ePreprinted)
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("ProductDetails.bnc_Products", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);

    cmd.Parameters.Add(new SqlParameter("@ProdId", SqlDbType.Int));
    cmd.Parameters["@ProdId"].Value = Convert.ToInt32(ProdId);
    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@Status"].Value = "Update";
    cmd.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.VarChar, 50));
    cmd.Parameters["@ProductName"].Value = eName;
    cmd.Parameters.Add(new SqlParameter("@Description", SqlDbType.VarChar, -1));
    cmd.Parameters["@description"].Value = eDescription;
    cmd.Parameters.Add(new SqlParameter("@Price", SqlDbType.Money));
    cmd.Parameters["@Price"].Value = ePrice;
    cmd.Parameters.Add(new SqlParameter("@Category", SqlDbType.Int));
    cmd.Parameters["@Category"].Value = eCat;
    cmd.Parameters.Add(new SqlParameter("@ProductType", SqlDbType.TinyInt));
    cmd.Parameters["@ProductType"].Value = eSCat;
    cmd.Parameters.Add(new SqlParameter("@Image", SqlDbType.Int));
    cmd.Parameters["@Image"].Value = eImage;
    cmd.Parameters.Add(new SqlParameter("@InStock", SqlDbType.Bit));
    cmd.Parameters["@InStock"].Value = eInstock;
    cmd.Parameters.Add(new SqlParameter("@PrePrinted", SqlDbType.Bit));
    cmd.Parameters["@PrePrinted"].Value = ePreprinted;

    try
    {
        con.Open();

        DataSet ds = new DataSet();
        adapter.Fill(ds);
    }
    catch (Exception err)
    {
        lblProdGrd.Text += err.Message;
    }
    finally
    {
        con.Close();
    }
}}

我的存储过程:

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

此外,当我尝试 删除 产品时,我们会宣布它已停产,但似乎什么也没发生,如果有人能够指出哪里出了问题,我会非常感谢,因为我对发生的事情一无所知。抱歉,如果其中任何一个看起来有点含糊,我是这方面的新手,所以目前处于完全停顿状态。提前感谢任何愿意提供帮助的人。

您得到 System.FormatException 因为已经打开了以下 asp:content 但还没有关闭它:

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

所以在 </asp:GridView> 之后输入 </asp:content>delete function 将被执行。