Error DataSource: "Unable to bind to property or column"

I am working on a database in C #, when I press the display button, I get an error:

Error:
Cannot associate a property or LastName column with a DataSource. Parameter Name: dataMember

the 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 that means, can anyone help if I comment on the last two lines, it will display correctly.

+6
source share
3 answers

this means your datatable does not find the LastName column name that is in your database.

in your case, you populate your dataset with ds2 ..

  Program.da2.Fill(Program.ds2); 

and then you bind your data source to a β€œprogram” like this.

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

he should like it ..

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

because below the line you are looking for a value from Program.tblNamesBS2 that is bound to 'ds' and why the column is not in 'ds'.

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

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

+5
source

I had the same problem, and this happened because the columns in both of my tables had the same column names. For instance:

  • My database name was Assets ;
  • The first table name was Property , and the second table name was Plants ;
  • The first column names in both tables were asset_number ; He gave the error described above, but mine said asset_number .

Decision. I changed the column names in my Plants table to asset_number1 and then it had no problems. (I had to delete all my old columns in Plants in order to redo the new columns.)

0
source

Source: https://habr.com/ru/post/921277/


All Articles