Development Desk

Just another WordPress.com site

Solution for Exception: System.InvalidOperationException: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

This exception occurs when we assign DataSource to some DataGrid or Calender and after that try to bind that DataSource with that Control.  Actually the  Problem is, Control like Gridview requires a collection to be bound with them instead of on single Object

suppose you have a Person class you fetch 1 single person from database like in below case person data is fetched by the method in the business Logic layer on the base of person id

Person singlePerson = new personBL().getSinglePerson(personId);

and then

personGridView.DataSource = singlePerson;

personGridView.DataBind(); // on this point exception will be thrown because single Instance of class is assigned to Gridview

it can be resolved, if you have only single object to assign you can simply add it to the list then assign to the DataSource like

 

Person singlePerson = new personBL().getSinglePerson(personId);

List <Person> personList = new List<Person>();

personList.Add(singlePerson );

personGridView.DataSource = personList;

personGridView.DataBind();

or

simply assign a collection instead of on single instance of object to resolve it

it just simple is that.

thank you

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: