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

DataSource error: quot;Cannot Bind to property or Columnquot;(数据源错误:“无法绑定到属性或列)

本文介绍了数据源错误:“无法绑定到属性或列"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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"));

这篇关于数据源错误:“无法绑定到属性或列"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:数据源错误:“无法绑定到属性或列"

基础教程推荐