I am just got madness for getting this for updating the table related with category table and product table …but i dint success in doing that …
I have these tables
product product_id
product_name
product_description
product_price
product_image
category category_id
catgory_name
category_desc
I have done like this for updating table ….using entity framework …
private void btnSave_Click(object sender, EventArgs e)
{
if (lblHiddenmode.Text == "Edit")
{
using (var dbcontext = new TsgEntities())
{
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
Image image = pictureBox1.Image;
byte[] bit = null;
bit = imageToByteArray(image);
product1 pd = new product1();
string category = cbcategorytypes.Text;
string categorydesc = tbCategoryDescription.Text;
var c = new category { category_Name = category, category_Description = categorydesc };
pd.product_Name = tbProductName.Text;
decimal price = Convert.ToDecimal(tbProductPrice.Text);
pd.product_Price = price;
pd.product_Description = tbProductdescription.Text;
pd.product_Image = bit;
pd.category = c;
dbcontext.SaveChanges();
this.Close();
}
}
}
NOTE: I am updating the product name which one has the product_id is 4 and category_id = 4
but it will shows at this statement pd.category = c; i got product_id = “0” and category_id = “0”
am i doing wrong when updating the table with category table .. is there any problem with update statement
Your code above will produce insert, not an update. In order to perform an update, you first need to retrieve a
product1instance from your context.I don’t know what your context looks like, so I can’t post the exact code, but it will be something like this:
You can then make your changes and call
dbcontext.SaveChanges()and it should update your record.