MissingMemberException

I've created a custom class (contacts_list) that inherits all the properties from a Linq Class (tb_contacts) and implements an own property (my_field):

public partial class contacts_list : tb_contacts
{
      private bool _my_field;
      public bool my_field
      {
            get { return _my_field; }
            set { _my_field = value; }
      }
}


Well, I fill an EntitySet<contacts_list> and I link this EntitySet<contacts_list> to the datasource of a datagridview.
The grid is filled perfectly, but when I click on the header of one of its columns to sort the records, I get a "MissingMemberException": "Impossible to find the member contacts_list.xxx".
Where "xxx" is the name of the header-column clicked.
I can improve to this error just only declaring in my class "contacts_list" all the properties from "tb_contacts". So my class will be:

public partial class contacts_list : tb_contacts
{
      public string name { get; set; }
      public string surname { get; set; }
      public string email { get; set; }

      private bool _my_field;
      public bool my_field
      {
            get { return _my_field; }
            set { _my_field = value; }
      }
}

But... it's a nonsense?!?!?!? All the properties should be inherited (in fact the datagridview is well filled)... so, I don't understand the exception...
demiketAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
No, it does not look like you made a mistake. The rules of inheritances are that all public members of a base class is accessible from the derived class and therefore the DataSource should be able to access it as qwell. But the workaround you have posted does the trick and it is simple.
0
 
Fernando SotoRetiredCommented:
Can you post the class tb_contacts ?
0
 
demiketAuthor Commented:
It's the automatic class created by LINQ from table tb_contact.

Here's the code, thank you.

[Table(Name="dbo.tb_contacts")]
public partial class tb_contacts : INotifyPropertyChanging, INotifyPropertyChanged
{
	private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
	
	private int _id_contact;
	private string _name;
	private string _surname;
	private string _email;
	
	#region Extensibility Method Definitions
	partial void OnLoaded();
	partial void OnValidate(System.Data.Linq.ChangeAction action);
	partial void OnCreated();
	partial void Onid_contactChanging(int value);
	partial void Onid_contactChanged();
	partial void OnnameChanging(string value);
	partial void OnnameChanged();
	partial void OnsurnameChanging(string value);
	partial void OnsurnameChanged();
	partial void OnemailChanging(string value);
	partial void OnemailChanged();
	#endregion
	
	public tb_contacts()
	{
		OnCreated();
	}
	
	[Column(Storage="_id_contact", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
	public int id_contact
	{
		get
		{
			return this._id_contact;
		}
		set
		{
			if ((this._id_contact != value))
			{
				this.Onid_contactChanging(value);
				this.SendPropertyChanging();
				this._id_contact = value;
				this.SendPropertyChanged("id_contact");
				this.Onid_contactChanged();
			}
		}
	}
	
	[Column(Storage="_name", DbType="NVarChar(255)")]
	public string name
	{
		get
		{
			return this._name;
		}
		set
		{
			if ((this._name != value))
			{
				this.OnnomeChanging(value);
				this.SendPropertyChanging();
				this._name = value;
				this.SendPropertyChanged("name");
				this.OnnameChanged();
			}
		}
	}
	
	[Column(Storage="_surname", DbType="NVarChar(255)")]
	public string surname
	{
		get
		{
			return this._surname;
		}
		set
		{
			if ((this._surname != value))
			{
				this.OnsurnameChanging(value);
				this.SendPropertyChanging();
				this._surname = value;
				this.SendPropertyChanged("surname");
				this.OnsurnameChanged();
			}
		}
	}
	
	[Column(Storage="_email", DbType="NVarChar(255)")]
	public string email
	{
		get
		{
			return this._email;
		}
		set
		{
			if ((this._email != value))
			{
				this.OnemailChanging(value);
				this.SendPropertyChanging();
				this._email = value;
				this.SendPropertyChanged("email");
				this.OnemailChanged();
			}
		}
	}
	
	public event PropertyChangingEventHandler PropertyChanging;
	public event PropertyChangedEventHandler PropertyChanged;
	
	protected virtual void SendPropertyChanging()
	{
		if ((this.PropertyChanging != null))
		{
			this.PropertyChanging(this, emptyChangingEventArgs);
		}
	}
	
	protected virtual void SendPropertyChanged(String propertyName)
	{
		if ((this.PropertyChanged != null))
		{
			this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Fernando SotoRetiredCommented:
Can you post the code that does this, "Well, I fill an EntitySet<contacts_list> and I link this EntitySet<contacts_list> to the datasource of a datagridview."?

Also how do you create the elements of the EntitySet<contacts_list>?
0
 
demiketAuthor Commented:
This is an example: the situation is more complex.
This is the function ContactsList to obtain the EntitySet<contacts_list>: so I put my datagridview.datasource=ContactsList.
public EntitySet<contacts_list> ContactsList
{
	EntitySet<contacts_list> _contactsList = new EntitySet<contacts_list>();
	var query = 
		from a in datacontext1.tb_contacts
		select new contacts_list
		{
			id_contact = a.tb_contacts.id_contact,
			name = a.tb_contacts.name,
			surname = a.tb_contacts.surname,
			email = a.tb_contacts.email,
			my_field = false
		};
	_contactsList.AddRange(query);
	return _contactsList;
}

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi demiket;

Try and return a List<contacts_list> rather then an EntitySet<contacts_list>. See code snippet.

Fernando
public List<contacts_list> ContactsList()
{
    List<contacts_list> _contactsList = new List<contacts_list>();
 
    var query = 
        from a in datacontext1.tb_contacts
        select new contacts_list
        {
            id_contact = a.id_contact,
            name = a.name,
            surname = a.surname,
            email = a.email,
            my_field = false
        };
    _contactsList.AddRange(query);
    return _contactsList;
}

Open in new window

0
 
demiketAuthor Commented:
A list is not sortable: so when I link the list to the datasource of a datagridview, I can't sort the datagridview by the headers of the columns...
0
 
Fernando SotoRetiredCommented:
Sorry I have looked at this and can not find an answer to why the public properties of the base class can not be accessed through contacts_list when assigned to the data source of the DataGridView, might be a bug?
0
 
demiketAuthor Commented:
Wonderful, grrrrrrrrrr...
Might be a Bug?!?!?!?
I have thought in a Bug, but I'm just a newbie in Linq and C# so I thought I was making some mistakes...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.