C# – Both DataSource and DataSourceID are defined error using with ASP.NET GridView

asp.netcdatasourcegridviewsqldatasource

Yes, i know. Possible dublicate question.

But it's different situation on gridview.

<asp:GridView ID="Grid_Goster" runat="server" AutoGenerateColumns="False" CellPadding="4" 
            DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None" 
            Height="144px" Width="316px">

<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SqlServerCstr %>" 
            SelectCommand="SELECT * FROM [AVUKAT]"></asp:SqlDataSource>

And i get error like this:

Both DataSource and DataSourceID are
defined on 'Grid_Goster'. Remove one
definition.
Description: An unhandled exception occurred during the
execution of the current web request.
Please review the stack trace for more
information about the error and where
it originated in the code.

Exception Details: System.InvalidOperationException: Both

DataSource and DataSourceID are
defined on 'Grid_Goster'. Remove one
definition.

Source Error:

An unhandled exception was generated during the execution of the

current web request. Information
regarding the origin and location of
the exception can be identified using

And in my .cs file there is code like this.

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            Grid_Goster.DataSource = dr;
            Grid_Goster.Visible = true;

i think this code works correctly.
How can i solve this DataSource and DataSourceID problem?

Best Answer

Maybe this is a dumb question; but it's good to ask just for clarification. Are you specifying both a datasource object (in your code behind) AND DatasourceID (in your ASPX markup) on the same control? I'd imagine that it wouldn't work when you try to Databind due to the Datasource ambiguity.

Related Topic