首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataBinding: System.Data.DataTable不允许索引访问

DataBinding: System.Data.DataTable不允许索引访问
EN

Stack Overflow用户
提问于 2017-02-02 19:24:00
回答 2查看 909关注 0票数 1

我一直收到下面的'System.ArgumentException‘错误,它读取

DataBinding:“System.Data.DataTable”不允许索引访问。

在我的GridView里。当我运行该项目时,它会进入html标记<asp:Label ID="lblTargetName" runat="server" Text='<%# Eval("[TargetName]") %>'></asp:Label>

这是HTML:

代码语言:javascript
复制
<asp:GridView>
    <asp:TemplateField HeaderText="TargetName" SortExpression="TargetName">
        <ItemTemplate>
            <asp:Label ID="lblTargetName" runat="server" Text='<%# Eval("[TargetName]") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

以及绑定网格的代码:

代码语言:javascript
复制
protected void UpdateGridview()
{
string PlanningType = DropDownList4.SelectedValue.ToString();
string ProductionYear = null;
//SqlDataSource sds = new SqlDataSource();
SqlConnection con = new SqlConnection(DatabaseConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

//sds = Page.FindControl("SqlDataSource1") as SqlDataSource;

if (DropDownList5.SelectedValue != "")
    ProductionYear = DropDownList5.SelectedValue.ToString();

try
{
    if (ProductionYear != null)
    {
        using (con)
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("sp_GetSUPPExcelImport", con);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PlanningType", SqlDbType.VarChar));
            cmd.Parameters["@PlanningType"].Value = PlanningType.ToString();
            cmd.Parameters.Add(new SqlParameter("@ProductionYear", SqlDbType.VarChar));
            cmd.Parameters["@ProductionYear"].Value = ProductionYear;                        

            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);

            GridView1.DataSource = ds.Tables;
            GridView1.AllowPaging = true;
            GridView1.DataBind();
        }
    }
}
catch (Exception ex)
{
    Label1.ForeColor = Color.Red;
    Label1.Text = ex.Message.ToString();
}
finally
{
    if (da != null)
        da.Dispose();

    if (ds != null)
        ds.Dispose();

    if (con != null)
    {
        con.Close();
        con.Dispose();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-02 21:11:59

您的GridView也缺少它的ID和runat命令。

<asp:GridView runat="server" ID="GridView1">

票数 1
EN

Stack Overflow用户

发布于 2018-08-28 20:30:54

我知道这是一个迟来的反应,但我今天遇到了这个例外,这是我发现的少数几个处理它的地方之一。这可能会帮助其他人找到他们的解决方案。

此问题与从Server复制数据元素以及"[]“有关。在问题中,OP试图绑定到标签,而数据绑定错误被抛出“不允许索引访问”。

注意Eval()语句中的括号。去掉托架,一切都会好的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42010663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档