Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按组合框导入ForeignKey值_C#_Winforms_Combobox - Fatal编程技术网

C# 按组合框导入ForeignKey值

C# 按组合框导入ForeignKey值,c#,winforms,combobox,C#,Winforms,Combobox,我试图使用ComboBox导入ForeignKey值,但是ComboBox加载字符串值,ForeignKey类型为int,我试图将转换为字符串(),然后出现错误: “赋值的左侧必须是变量属性或索引器” 如有任何帮助,我们将不胜感激。此行错误: newInvoice.SupplierId.ToString() = comboBox1.Text; 您正在尝试为方法调用赋值 相反,这一行应该是: newInvoice.SupplierId = Int32.Parse(comboBox1.Text);

我试图使用
ComboBox
导入
ForeignKey
值,但是
ComboBox
加载
字符串
值,
ForeignKey
类型为
int
,我试图将
转换为字符串()
,然后出现错误:

“赋值的左侧必须是变量属性或索引器”

如有任何帮助,我们将不胜感激。

此行错误:

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
}
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
}