Wednesday, October 15, 2008

Both DataSource and DataSourceID are defined on ‘GridView1′. Remove one definition.

This is the message that you get most of the times when you try to bind your gridview or any other bindable controls to multiple Datasources.When you bind your Gridview control to an ObjectDataSource or SqlDataSource on the design surface and then you try to bind the same Gridview in the code behind using the manual coding.

The Gridview has both Datasource and DatasourceID properties.Datasource referes to the ID of another component such as SqlDataSource/XMLDataSource/AccessDataSource, whichever applies, or ObjectDataSource to act as the Datasource.


When you have the following code:


SqlDataSource1.DataBind();
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();


You'll get this error:
Both DataSource and DataSourceID are defined on ‘GridView1′. Remove one definition.


To make working,

Just add a line of code before DataBind():
GridView1.DataSourceID = string.Empty;


Now, you are fine, no more error......


Happy Programming!!

4 comments:

Highly Appreciated your comments