未提供预期参数

时间:2015-02-03 15:38:07

标签: c# asp.net sql-server

我在asp.net网页中使用存储过程时遇到了一些问题 我创建了一个存储过程来插入/更新并声明产品已停产。 我收到以下错误: 预期参数没有提供。

我不确定为什么。任何帮助都会令人惊讶,因为我真的在努力解决这个问题

我的标记如下:

<asp:Label ID="lblProdGrd" runat="server"></asp:Label>
<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">
    <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='<%# Eval("Price") %>'></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">
                </asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstCat" runat="server" AppendDataBoundItems="True"
                    DataTextField='<%# Eval("CategoryName") %>' DataValueField='<%# Eval("CategoryId")%>'>
                    <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"></asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstAddSubCat" runat="server"></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"></asp:DropDownList>
                <asp:Image ID="imgProdEdit" runat="server" Height="250" Width="250" />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstAddProdImg" runat="server"></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

我的代码如下:

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




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, Price, Category, SubCat, Image; 
        bool InStock, Preprinted;

        ProductName = txtName.Text;
        Description = txtDescription.Text;
        Price = txtPrice.Text;
        Category = lstCatEdit.SelectedValue;
        SubCat = lstSubCat.SelectedValue;
        Image = 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, string Price, string Category, string SubCat, 
    string 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", "Add"));
    cmd.Parameters["@status"].Value = "";
    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");

    string pId = prId.Text;

    DeleteProduct(pId);

    grdProds.EditIndex = -1;

    BindGrid();

}
protected void DeleteProduct(string 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");

    string ProdId = prdId.Text;
    string eName = Name.Text;
    string eDescription = Description.Text;
    string ePrice = Price.Text;
    string eCat = Category.SelectedValue;
    string eSCat = SubCat.SelectedValue;
    string eImage = 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(string ProdId, string eName, string eDescription, string ePrice, string eCat, string eSCat, string 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 = ProdId;
    cmd.Parameters.Add(new SqlParameter("@Status", "Update"));
    cmd.Parameters["@Status"].Value = "Update";
    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,
@Category int,
@ProductType tinyint, 
@Price money,
@InStock bit = 1,
@ProductName varchar(50),
@Image int,
@Description varchar(max),
@PrePrinted bit = 1,
@Discontinued bit = 0,
@Status varchar(50)
AS 
BEGIN 
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

任何人都能帮我解决这个问题的任何帮助都会很精彩,因为我已经对此感到头疼了几天,并且没有更接近解决问题。如果我没有提供您可能需要的所有信息,我很抱歉,请不要犹豫,我会提供。

1 个答案:

答案 0 :(得分:0)

我认为您只尝试传递@Status。我对吗?如果是,则在存储过程中,您需要使用默认空值定义参数,这意味着您可以传递或不传递null参数,.net不会出现此错误。

@ProdId int = null,
@Category int= null,
@ProductType tinyint = null, 
@Price money = null,
@InStock bit = 1,  --for this you already mention what is default value. so no need to change. Same thing for below.
@ProductName varchar(50) = null,
@Image int = null,
@Description varchar(max) = null,
@PrePrinted bit = 1,
@Discontinued bit = 0,
@Status varchar(50) = null