0

我正在尝试使用 导入ForeignKeyComboBox,但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();

任何帮助,将不胜感激。

4

1 回答 1

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
}
于 2013-05-21T11:45:07.857 回答