C# 数据源错误:“无法绑定到属性或列”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11645551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:39:03  来源:igfitidea点击:

DataSource error: "Cannot Bind to property or Column"

c#dataset

提问by Kay

I'm working on a database in C# when I hit the display button I get an error:

当我点击显示按钮时,我正在使用 C# 处理数据库,但出现错误:

Error:
Cannot bind to the property or column LastName on the DataSource. Parameter name: dataMember

错误:
无法绑定到数据源上的属性或列姓氏。参数名称:dataMember

Code:

代码:

private void Display_Click(object sender, EventArgs e)
{
    Program.da2.SelectCommand = new SqlCommand("Select * From Customer", Program.cs);
    Program.ds2.Clear();
    Program.da2.Fill(Program.ds2);
    customerDG.DataSource = Program.ds2.Tables[0];

    Program.tblNamesBS2.DataSource = Program.ds.Tables[0];

    customerfirstname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "FirstName"));
    customerlastname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "LastName")); //Line Error occurs on.
}

Not sure what it means can anyone help, if I comment out the last two lines it will display properly.

不知道这意味着什么任何人都可以提供帮助,如果我注释掉最后两行,它将正确显示。

采纳答案by user1102001

it means your datatable is not finding column name LastName which is in your database..

这意味着您的数据表没有找到您数据库中的列名 LastName ..

in your case you filling your dataset with ds2..

在你的情况下,你用 ds2 填充你的数据集..

 Program.da2.Fill(Program.ds2); 

and then you are binding your datasource to 'program' like this..

然后你将你的数据源绑定到“程序”,就像这样..

Program.tblNamesBS2.DataSource = Program.ds.Tables[0];  

it should like this..

应该是这样的。。

Program.tblNamesBS2.DataSource = Program.ds2.Tables[0];  

because below line you are looking value from Program.tblNamesBS2 which is binded to 'ds' and that's why column are not ther in 'ds'.

因为在下面的行中,您正在查看绑定到“ds”的 Program.tblNamesBS2 的值,这就是为什么列不在“ds”中的原因。

 customerfirstname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "FirstName"));    
  customerlastname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "LastName"));

回答by Brien King

You will also run into this error if you bind to a NULL object.

如果绑定到 NULL 对象,也会遇到此错误。

回答by Charl Beukes

I had the same problem and it was because the columns in both my tables had the same column names. For example:

我遇到了同样的问题,这是因为我的两个表中的列具有相同的列名。例如:

  1. My database name was Assets;
  2. The first table name was Propertyand the second table name was Plants;
  3. The first column names in both the tables were asset_number; It gave the error you describe above, but mine said asset_number.
  1. 我的数据库名称是Assets;
  2. 第一个表名是Property,第二个表名是Plants
  3. 两个表中的第一个列名都是asset_number; 它给出了你上面描述的错误,但我的说asset_number

Solution: I changed the column names in my table Plantsto asset_number1and then it had no problem. (I did have to delete all my old columns in Plantsto redo the new columns.)

解决方案:我将表中的列名更改Plantsasset_number1,然后就没有问题了。(我确实必须删除所有旧列Plants才能重做新列。)

回答by Lost Kid

I run into the same problem where I have to change the name in the table but I haven't change column names in the data binding file. solution was changing the names by replacing the changed name.

我遇到了同样的问题,我必须更改表中的名称,但我没有更改数据绑定文件中的列名。解决方案是通过替换更改后的名称来更改名称。

回答by Mike Cheel

Another reason for this error is if the property you are binding to is private.

此错误的另一个原因是您绑定的属性是否是私有的。