我正在尝试使用ForeignKey
导入ComboBox
值,但ComboBox
加载string
值且ForeignKey
类型为int
我尝试转换ToString()
,然后收到错误:
“作业的左侧必须是变量属性或索引器”
ShippingDocumentDataClassesDataContext dc = new ShippingDocumentDataClassesDataContext();
t_tracking newInvoice = new t_tracking();
newInvoice.SupplierId.ToString() = comboBox1.Text;
dc.t_trackings.InsertOnSubmit(newInvoice);
dc.SubmitChanges();
任何帮助都将不胜感激。
答案 0 :(得分:0)
这一行错了:
newInvoice.SupplierId.ToString() = comboBox1.Text;
您正在尝试为方法调用分配值。
相反,这一行应该是:
newInvoice.SupplierId = Int32.Parse(comboBox1.Text);
或更安全的方式:
int id = 0;
if (Int32.TryParse(comboBox1.Text, out id))
{
//we get valid integer from combobox
newInvoice.SupplierId = id;
dc.t_trackings.InsertOnSubmit(newInvoice);
dc.SubmitChanges();
}
else
{
//wrong value handling code goes here
}