DataSource和DataSourceID都在GridView1'上定义。删除webforms

时间:2018-04-26 13:16:00

标签: c# asp.net webforms

我收到上面提到的错误,即使我没有在我的代码中的任何地方使用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">

C#代码:

if (ShouldWeBindToGridView == true)
{
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

我做错了什么?

更新

这是所要求的完整代码:

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

这是C#代码:

 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();


            }
        }
    }

正如您所看到的,我根本没有使用DataSourceID,而是在C#Code中绑定数据。

错误消息:

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

2 个答案:

答案 0 :(得分:1)

我在这里看到几个问题,我将添加驴评论。

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();
    }
}

答案 1 :(得分:0)

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