Wayne Hollier
Wayne Hollier

Reputation: 65

asp:GridView DataBinding: 'System.Data.DataTable' does not allow indexed access

I keep getting the following 'System.ArgumentException' error which reads

DataBinding: 'System.Data.DataTable' does not allow indexed access.

in my GridView. When I run the project it breaks into the html tag <asp:Label ID="lblTargetName" runat="server" Text='<%# Eval("[TargetName]") %>'></asp:Label>

This is the 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>

and the code behind to bind the grid:

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

Upvotes: 1

Views: 1074

Answers (2)

Fio
Fio

Reputation: 40

I know this is a late response but I ran into this exception today and this was one of very few places I found that dealt with it. This may help others figure out their solution.

This problem is related to having copied the data element over from SQL Server along with the "[]". In the question the OP attempts to bind to a label and the data binding error gets thrown "does not allow indexed access".

Note the brackets in the Eval() statement. Remove the brackets and all will be well.

Upvotes: 1

Ginjo
Ginjo

Reputation: 26

Your GridView is also missing its ID and runat command.

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

Upvotes: 1

Related Questions