数据源错误:“无法绑定到属性或列"数据源、绑定、属性、错误

由网友(旒浪ьёi方)分享简介:我正在使用 C# 处理数据库,当我点击显示按钮时出现错误:I'm working on a database in C# when I hit the display button I get an error:错误:无法绑定到 DataSource 上的属性或列 LastName.参数名称:dataMemberE...

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

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

错误:无法绑定到 DataSource 上的属性或列 LastName.参数名称:dataMember

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

代码:

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.

推荐答案

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

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

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

in your case you filling your dataset with ds2..

 Program.da2.Fill(Program.ds2); 

然后您将数据源绑定到这样的程序"..

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

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

应该是这样的..

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

因为您正在从绑定到ds"的 Program.tblNamesBS2 中寻找价值这就是为什么ds"中没有列的原因.

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'.

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

相关推荐

最新文章