C# DataSource和DataSourceID都是在';GridView1';。删除webforms中的一个定义

C# DataSource和DataSourceID都是在';GridView1';。删除webforms中的一个定义,c#,asp.net,webforms,C#,Asp.net,Webforms,即使我没有在代码中的任何地方使用DataSourceID,并且我确保没有从代码隐藏和客户端绑定数据,我也会遇到上面提到的错误 标记: <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" Horizon

即使我没有在代码中的任何地方使用DataSourceID,并且我确保没有从代码隐藏和客户端绑定数据,我也会遇到上面提到的错误

标记:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
            HorizontalAlign="Center" OnPageIndexChanging="GridView1_PageIndexChanging" 
            OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
            EnableModelValidation="True">
if (ShouldWeBindToGridView == true)
{
    GridView1.DataSource = ds;
    GridView1.DataBind();
}
 private void Load_Grid(bool ShouldWeBindToGridView)
    {
        DataSet ds = new DataSet();

            SqlConnection conn = new SqlConnection(strcnn);

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_Students", conn);
                cmd.CommandTimeout = 120;
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch
            { }
            finally
            {
                conn.Close();
            }

        if (ds != null)
        {
            if (ShouldWeBindToGridView == true)
            {

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


            }
        }
    }
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: 


[InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.]
   System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +10967460
   System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) +28
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428
我做错了什么

更新:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
            HorizontalAlign="Center" OnPageIndexChanging="GridView1_PageIndexChanging" 
            OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
            EnableModelValidation="True">
if (ShouldWeBindToGridView == true)
{
    GridView1.DataSource = ds;
    GridView1.DataBind();
}
 private void Load_Grid(bool ShouldWeBindToGridView)
    {
        DataSet ds = new DataSet();

            SqlConnection conn = new SqlConnection(strcnn);

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_Students", conn);
                cmd.CommandTimeout = 120;
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch
            { }
            finally
            {
                conn.Close();
            }

        if (ds != null)
        {
            if (ShouldWeBindToGridView == true)
            {

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


            }
        }
    }
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: 


[InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.]
   System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +10967460
   System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) +28
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428
这是请求的完整代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" HorizontalAlign="Center"
                        OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound"
                        OnSorting="GridView1_Sorting"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" EnableModelValidation="True">

                        <Columns>

                            <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" Visible="False" />

                             <asp:TemplateField HeaderText="ID Number" SortExpression="Student_IDNumber">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ID1") %>'></asp:TextBox>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ID2") %>'></asp:TextBox>
                                    <%--<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ID3") %>'></asp:TextBox>

                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID1") %>'></asp:Label> <br />
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("ID2") %>'></asp:Label> <br />
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ID3") %>'></asp:Label> 

                                </ItemTemplate>
                            </asp:TemplateField>

                        </Columns>
                        <RowStyle BackColor="#EFF3FB" Font-Names="Tahoma" Font-Size="8pt" HorizontalAlign="Left"
                            VerticalAlign="Top" />
                        <EditRowStyle BackColor="#2461BF" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" Font-Names="Tahoma" Font-Size="8pt"
                            ForeColor="White" HorizontalAlign="Center" VerticalAlign="Top" />
                        <AlternatingRowStyle BackColor="White" Font-Names="Tahoma" Font-Size="8pt" HorizontalAlign="Left"
                            VerticalAlign="Top" />
 </asp:GridView>
正如您所看到的,我根本没有使用DataSourceID,我只是用C#代码绑定数据

错误消息:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
            HorizontalAlign="Center" OnPageIndexChanging="GridView1_PageIndexChanging" 
            OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
            EnableModelValidation="True">
if (ShouldWeBindToGridView == true)
{
    GridView1.DataSource = ds;
    GridView1.DataBind();
}
 private void Load_Grid(bool ShouldWeBindToGridView)
    {
        DataSet ds = new DataSet();

            SqlConnection conn = new SqlConnection(strcnn);

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_Students", conn);
                cmd.CommandTimeout = 120;
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch
            { }
            finally
            {
                conn.Close();
            }

        if (ds != null)
        {
            if (ShouldWeBindToGridView == true)
            {

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


            }
        }
    }
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: 


[InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.]
   System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +10967460
   System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) +28
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428

我在这里看到了几个问题,我将添加一些评论

private void Load_Grid(bool ShouldWeBindToGridView)
{
 //as first whats the point of calling this method and wasting time reading database 
 //the parameter ShouldWeBindToGridView should be used before this whole method.

  DataSet ds = new DataSet();

  //Whenever you can use using USE IT!. it will automatically close connection and dispose.
  using(SqlConnection conn = new SqlConnection(strcnn)){
       conn.Open();
       using(SqlCommand cmd = new SqlCommand("sp_Students", conn)){
         cmd.CommandTimeout = 120;
         cmd.CommandType = CommandType.StoredProcedure;

         using(SqlDataAdapter adapter = new SqlDataAdapter(cmd)){
           adapter.Fill(ds);
         }
     }
  }

  //if(ds != null) does nothing you just made a new dataset it will be never null. Instead check if it contains any tables and any rows

  if(ds.Tables[0] != null)
    if(ds.Tables[0].Rows.Count != 0){
      GridView1.DataSource = ds;
      GridView1.DataBind();
    }else{
      GridView1.DataSource = null;
      GridView1.DataBind();
    }
}

我找到了问题的原因。在后端,存储过程是多个联合查询的组合。列的数量不一样,我在存储过程中遇到了一个错误,因此数据集总是空的,但我不知道为什么当问题由于存储过程中的问题而具有空数据集时,该错误与多个数据绑定有关。我想分享解决方案,以防其他人遇到同样的问题,而他们不想检查存储过程或后端代码。

请澄清您的意思,说“我没有绑定来自代码后台和客户端的数据”@HarutyunImirzyan我的意思是,我没有在标记中使用DataSourceID,也没有在C#代码中同时使用DataSource来绑定数据。你可以共享完整的代码隐藏吗?@HarutyunImirzyan我按照你的要求共享了完整的代码。谢谢,但这不是代码隐藏,而是aspx标记。请共享.cs文件内容,我也尝试了您上面建议的解决方案。没有错误,但也没有显示数据。@ITDeveloper在sqldatadapter上设置断点。你在de数据集中有数据吗?nvm在这评论之后我看到了你的答案。