我一直收到下面的'System.ArgumentException‘错误,它读取
DataBinding:“System.Data.DataTable”不允许索引访问。
在我的GridView里。当我运行该项目时,它会进入html标记<asp:Label ID="lblTargetName" runat="server" Text='<%# Eval("[TargetName]") %>'></asp:Label>。
这是HTML:
<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>以及绑定网格的代码:
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();
}
}发布于 2017-02-02 21:11:59
您的GridView也缺少它的ID和runat命令。
<asp:GridView runat="server" ID="GridView1">
发布于 2018-08-28 20:30:54
我知道这是一个迟来的反应,但我今天遇到了这个例外,这是我发现的少数几个处理它的地方之一。这可能会帮助其他人找到他们的解决方案。
此问题与从Server复制数据元素以及"[]“有关。在问题中,OP试图绑定到标签,而数据绑定错误被抛出“不允许索引访问”。
注意Eval()语句中的括号。去掉托架,一切都会好的。
https://stackoverflow.com/questions/42010663
复制相似问题