DataSource error: "Cannot Bind to property or Column"
Asked Answered
B

6

12

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

Error:
Cannot bind to the property or column LastName on the DataSource. Parameter name: 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.

Bayles answered 25/7, 2012 at 8:24 Comment(8)
Have you checked that LastName is correctly typed?Triturable
It means there is no column called LastName in the that table.Disulfiram
Very odd, on my SQL server manager the colum name is indeed "LastName" just like what i have typed. if i just comment out the last line it works, FirstName is correct and i spelt LastName in the same manner.Bayles
The DataSource of Program.tblNamesBS2 is Program.ds.Tables[0]. But you have filled the results to another Dataset Program.ds2. Is this correct?Malmsey
I have 2 Data sets. Program.TblNamesBS is Program.ds.Tables[0]. and i also have Program.tblNamesBS2 is Program.ds2.Tables[0]. No 2 is used for my customer form and the orginal is used for my contact Form ive used the same method.Bayles
Ahh ty Mohammad that fixed it. Should have been Program.ds2.Tables[0]Bayles
Slight prob if i click display it loads the table, but if i click display once again i get this error msg, is there a way to prevent this.. This causes two bindings in the collection to bind to the same property. Parameter name: bindingBayles
I think you will have to clear the bindings before adding new bindings. And khan is this a windows application?Malmsey
O
7

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

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];  

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

 customerfirstname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "FirstName"));    
  customerlastname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "LastName"));
Otero answered 25/7, 2012 at 8:31 Comment(1)
Why would this be? it can find FirstName, and LastName is spelt properly.Bayles
J
15

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

Journalism answered 29/4, 2015 at 3:7 Comment(1)
Create a new instance of the object and bind to that.Journalism
O
7

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

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];  

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

 customerfirstname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "FirstName"));    
  customerlastname.DataBindings.Add(new Binding("Text", Program.tblNamesBS2, "LastName"));
Otero answered 25/7, 2012 at 8:31 Comment(1)
Why would this be? it can find FirstName, and LastName is spelt properly.Bayles
U
5

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

Utica answered 17/3, 2020 at 16:14 Comment(0)
R
2

Yet another possible reason for this (if you bind to an object) is that you try to bind to a field, not a property.

Redtop answered 21/2, 2022 at 12:19 Comment(1)
Thanks Michael. I just tripped over this.Yoshi
K
0

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 Property and 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.

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

Kennedy answered 21/11, 2015 at 22:29 Comment(0)
A
0

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.

Archegonium answered 30/5, 2018 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.