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

Not able to update data in update page

I am working on updating gridview value by sending gridview values to other page. Here, I am able to update category dropdown list value and image file only. I am not able to update textbox values.

Here is my code..

Update Product Code:

protected void SubmitButton_Click(object sender, EventArgs e)
{
int productId = Convert.ToInt32(Request.QueryString["ProductID"]);

Product product = new Product();

product.ProductID = productId;
product.ProductName = TitleTextBox.Text;
product.Description = DescriptionTextBox.Text;
product.ItemsInSet = Convert.ToInt32(SetTextBox.Text);
product.UnitPriceOwner = Convert.ToInt32(UnitResellerTextBox.Text);
product.UnitPriceReseller = Convert.ToInt32(UnitResellerTextBox.Text);
product.ShippingCost = Convert.ToInt32(ShipmentTextBox.Text);
product.CategoryID = Convert.ToInt32(CategoryDropDownList.SelectedValue.ToString());
product.Visible = true;
product.InOffer = false;

if (ImageUpload.HasFile)
{
int length = ImageUpload.PostedFile.ContentLength;
product.ProductImage = new byte[length];
ImageUpload.PostedFile.InputStream.Read(product.ProductImage, 0, length);
}
else
{
Byte[] image;

image = ProductBL.GetImage(productId);
product.ProductImage = image;
}

ProductBL.UpdateProduct(product);
MessageLabel.Text = "Product successfully Updated!";
}


UpdateProduct() code:

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.Add("@ProductImage", SqlDbType.Image).Value = product.ProductImage == null ? (object)DBNull.Value : product.ProductImage;

DbUtility.UpdateDb(cmd);
}


Page Load Code:

protected void Page_Load(object sender, EventArgs e)
{
int productId = 0;
productId = Convert.ToInt32(Request.QueryString["ProductID"]);

LoadProductDetails(productId);
}

protected void LoadProductDetails(int productId)
{
var product = ProductBL.GetProductById(productId);

TitleTextBox.Text = product.ProductName;
DescriptionTextBox.Text = product.Description;
SetTextBox.Text = Convert.ToInt32(product.ItemsInSet).ToString();
UnitOwnerTextBox.Text = Convert.ToInt32(product.UnitPriceOwner).ToString();
UnitResellerTextBox.Text = Convert.ToInt32(product.UnitPriceReseller).ToString();
ShipmentTextBox.Text = Convert.ToInt32(product.ShippingCost).ToString();
}


Kindly Help me

Answer

Try to load your LoadProductDetails in

if(!IsPostback)
{
//here
}