【发布时间】:2011-08-24 16:18:30
【问题描述】:
我有两张桌子
产品
product_id
product_Name
product_Price
product_Description
product_image
category_id
另一个表
category
category_id
category_name
category_description
我有一个包含三个文本框的表单(比如 tbProductPrice、tbProductName、tbProductdescription)一个组合框(cbcategorytypes)两个按钮一个编辑,另一个是保存按钮.. 我正在尝试更新产品表以及 category_id
当我单击编辑按钮时,类别名称会加载到组合框中
当我们单击保存按钮时,文本框中的任何值将在产品表中与类别一起更新,我已经完成了以下代码...
using (var vareditcontext = new abcEntities())
{
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
Image image = pictureBox1.Image;
byte[] bit = null;
bit = imageToByteArray(image);
product1 pd = vareditcontext.product1.Where(p => p.product_Id == productid
&& p.category_Id == productcategoryid).First();
string category = cbcategorytypes.Text;
var c = new category { category_Name = category }; //problem at this line
pd.category = c;
pd.product_Name = tbProductName.Text;
decimal price = Convert.ToDecimal(tbProductPrice.Text);
pd.product_Price = price;
pd.product_Description = tbProductdescription.Text;
pd.product_Image = bit;
vareditcontext.SaveChanges();
this.Close();
}
当我单击保存按钮时,我遇到了这样的异常..
参数超出范围异常 ..
我收到此错误是因为当我编辑并尝试将产品详细信息与类别名称一起保存时,新的类别名称将存储在数据库中.....而不是更新当前的...
我该如何解决这个问题..我的意思是不存储我想将已经存在的类别设置到产品的新项目...
linq 可以吗 ....
任何人都可以帮助解决这个问题..
非常感谢....
【问题讨论】:
标签: c# .net linq entity-framework linq-to-entities