Orion Orion - 3 months ago 9
ASP.NET (C#) Question

The parameterized query expects the parameter '@ProductImage', which was not supplied

Here, when I try to update image in a gridview, I can update everything pretty easily. When I do not select image and try to update other fields, I get above error.

Here is my Code.

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();
}


This is my Business Logic Layer code for Product.

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);
}

Div Div
Answer

You can check like this before update:

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