2
Здесь, когда я пытаюсь обновить изображение в gridview, я могу легко обновить все. Когда я не выбираю изображение и не пытаюсь обновить другие поля, я получаю выше ошибки.Параметрированный запрос ожидает параметр «@ProductImage», который не был отправлен
Вот мой код.
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] [email protected], [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);
}
Возможно, вы имели в виду _product.ProductImage == null_ и для полноты добавить также еще часть – Steve
Да, моя ошибка. Обновлено –
Теперь я получаю эту ошибку. Дополнительная информация: Столкновение типа Operand: nvarchar несовместим с изображением. – Orion