gpt4 book ai didi

c# - 参数化查询需要参数 '@ProductImage' ,但未提供

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:25 25 4
gpt4 key购买 nike

在这里,当我尝试更新 GridView 中的图像时,我可以非常轻松地更新所有内容。当我不选择图像并尝试更新其他字段时,出现上述错误。

这是我的代码。

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int productId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

TextBox productName = GridView1.Rows[e.RowIndex].FindControl("ProductTextBox") as TextBox;
TextBox description = GridView1.Rows[e.RowIndex].FindControl("DescriptionTextBox") as TextBox;
TextBox itemsInSet = GridView1.Rows[e.RowIndex].FindControl("ItemsTextBox") as TextBox;
TextBox unitPriceOwner = GridView1.Rows[e.RowIndex].FindControl("PriceOwnerTextBox") as TextBox;
TextBox unitPriceReseller = GridView1.Rows[e.RowIndex].FindControl("PriceResellerTextBox") as TextBox;
TextBox shippingCost = GridView1.Rows[e.RowIndex].FindControl("CostTextBox") as TextBox;
TextBox inOffer = GridView1.Rows[e.RowIndex].FindControl("InOfferTextBox") as TextBox;
CheckBox visible = GridView1.Rows[e.RowIndex].FindControl("VisibleCheckBox") as CheckBox;

FileUpload FileUpload1 = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");

Product product = new Product();
product.ProductID = productId;
product.ProductName = productName.Text;
product.Description = description.Text;
product.ItemsInSet = Convert.ToInt32(itemsInSet.Text);
product.UnitPriceOwner = Convert.ToInt32(unitPriceOwner.Text);
product.UnitPriceReseller = Convert.ToInt32(unitPriceReseller.Text);
product.ShippingCost = Convert.ToInt32(shippingCost.Text);
product.InOffer = Convert.ToBoolean(inOffer.Text);
product.Visible = visible.Checked;

if (FileUpload1.HasFile)
{
int length = FileUpload1.PostedFile.ContentLength;
product.ProductImage = new byte[length];
FileUpload1.PostedFile.InputStream.Read(product.ProductImage, 0, length);
}
else
{
ProductBL.GetImage(productId);
}

ProductBL.UpdateProduct(product);

GridView1.EditIndex = -1;
GridView1.DataSource = ProductBL.GetProducts();
GridView1.DataBind();
}

这是我的产品业务逻辑层代码。

public static void UpdateProduct(Product product)
{
string query = "UPDATE [Products] SET [ProductName] = @ProductName, [Description] = @Description, [ItemsInSet] = @ItemsInSet, " +
"[UnitPriceOwner] = @UnitPriceOwner, [UnitPriceReseller] = @UnitPriceReseller, [CategoryID] = @CategoryID, " +
"[ShippingCost] = @ShippingCost, [InOffer] = @InOffer, [ProductImage] =@ProductImage, [Visible] = @Visible WHERE [ProductID] = @ProductID";
SqlCommand cmd = new SqlCommand(query);

cmd.Parameters.AddWithValue("@ProductName", SqlDbType.Text).Value = product.ProductName;
cmd.Parameters.AddWithValue("@Description", SqlDbType.Text).Value = product.Description;
cmd.Parameters.AddWithValue("@ItemsInSet", SqlDbType.Int).Value = product.ItemsInSet;
cmd.Parameters.AddWithValue("@UnitPriceOwner", SqlDbType.Int).Value = product.UnitPriceOwner;
cmd.Parameters.AddWithValue("@UnitPriceReseller", SqlDbType.Int).Value = product.UnitPriceReseller;
cmd.Parameters.AddWithValue("@CategoryID", SqlDbType.Int).Value = product.CategoryID;
cmd.Parameters.AddWithValue("@ShippingCost", SqlDbType.Int).Value = product.ShippingCost;
cmd.Parameters.AddWithValue("@InOffer", SqlDbType.Bit).Value = product.InOffer;
cmd.Parameters.AddWithValue("@Visible", SqlDbType.Bit).Value = product.Visible;
cmd.Parameters.AddWithValue("@ProductID", SqlDbType.Text).Value = product.ProductID;
cmd.Parameters.AddWithValue("@ProductImage", SqlDbType.Image).Value = product.ProductImage;

DbUtility.UpdateDb(cmd);
}

最佳答案

更新前可以这样检查:

SqlParameter unitsParam = command.Parameters.AddWithValue("@ProductImage", SqlDbType.Image);
if (product.ProductImage == null)
{
unitsParam.Value = DBNull.Value;
}
else
{
unitsParam.Value = product.ProductImage
}

关于c# - 参数化查询需要参数 '@ProductImage' ,但未提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39205049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com