The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource

1

Why i am getting following error ? i can see data when i did a test run query !

Server Error in '/' Application.
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.  A control with ID 'MyDataSource' could not be found.
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.Web.HttpException: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.  A control with ID 'MyDataSource' could not be found.

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 the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.  A control with ID 'MyDataSource' could not be found.]
   System.Web.UI.WebControls.DataBoundControl.GetDataSource() +8556294
   System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +37
   System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) +19
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927 

Below is my source :

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="MyDataSource">
        <Columns>
            <asp:BoundField DataField="Colum1" HeaderText="Colum1" 
                SortExpression="Colum1" />
            <asp:BoundField DataField="Column2" HeaderText="Column2" 
                SortExpression="Column2" />
            <asp:BoundField DataField="Column3" HeaderText="Column3" 
                SortExpression="Column3" />
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>
c#
.net
asp.net
gridview
ado.net
asked on Stack Overflow Apr 19, 2011 by SmartestVEGA • edited Apr 19, 2011 by SmartestVEGA

5 Answers

5

The problem is that there is no datasource control on the form with the ID MyDataSource which is the value of the DataSourceID property of the GridView.

<asp:GridView ID="GridView1" runat="server" DataSourceID="MyDataSource"></asp:GridView>
<asp:SqlDataSource ID="MyDataSource" runat="server"></asp:SqlDataSource>

In the above example, I used a SqlDataSource. However, any datasource control will do and its ID value must match the value used in the DataSourceID attribute of the GridView.

Another thing to check is that the markup is valid. I.e., there are no missing angle brackets, nothing outside the form tag. Nothing on a content page with a Master page that is outside of a content area. etc.

answered on Stack Overflow Apr 19, 2011 by Thomas
2

This can also happen when the page uses a masterpage and the ObjectDataSource is in the scope of a different asp:content control

answered on Stack Overflow Oct 3, 2013 by Filip
0

DataSourceID should be the ID of a datasource control on your page. If you are binding in the codebehind then do not set the DatasourceID.

answered on Stack Overflow Apr 19, 2011 by Chad
0

just remove this from the gridview :

DataSourceID="MyDataSource"

if you fill it from the codebehind

answered on Stack Overflow Dec 1, 2016 by albaiti
-2

first goto App_Data and create xml file

  • 1-App_Data/ddlxmlFile.xml

  • 2-Get xmldatasource from toolbox and give same datasourceid name to xmldatasource ID=""

apply this in your code

answered on Stack Overflow Jun 23, 2018 by Sanket Jambhulkar

User contributions licensed under CC BY-SA 3.0