Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
The code below is an example of my original which is receiving the listed error in the title of this post.

VB
Dim dt As New DataTable
    dt.Clear()
        dt.Columns.Add("apple")
        dt.Columns.Add("apple 1")
    Dim mr As DataRow
        mr = dt.NewRow
        mr("apple") = "Macbook"
        mr("apple 1") = "ipod"
    dt.Rows.Add(mr)
    GridView1.DataSource = dt
    GridView1.DataBind()


What I have tried:

Googling this error buy my error is just different from all.
Posted
Updated 10-May-16 10:55am
v2

1 solution

Spaces in column names are troublesome!
If you must use the space in the name, then the column name should be enclosed in square brackets [].
Alternatively, remove the space from the ColumnName and use the Caption property of the DataColumn for the friendly name.

===== Here's the C# of what I tried ==========
C#
using System;
using System.Data;
using System.Web.UI;

namespace CP_WebDataColumn
{
  public partial class About : Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      DataTable dt = new DataTable();
      dt.Clear();
      dt.Columns.Add("Apple");
      DataColumn col = dt.Columns.Add("Apple 1");
      DataRow mr = dt.NewRow();
      mr["Apple"] = "Macbook";
      mr["Apple 1"] = "ipod";
      dt.Rows.Add(mr);
      GridView1.DataSource = dt;
      GridView1.DataBind();
    }
  }
}


Here's the About.aspx:
ASP.NET
<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="CP_WebDataColumn.About" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <h3>Your application description page.</h3>
    <p>Use this area to provide additional information.</p>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
</asp:Content>
 
Share this answer
 
v2
Comments
JT1992 10-May-16 17:05pm    
Thank you Matt. How would i enclose brackets in "apple" or "apple 1" because i have to have a space for this property value.
Matt T Heffron 10-May-16 17:08pm    
dt.Columns.Add("[apple 1]")
and
mr("[apple 1]") = "ipod"
JT1992 10-May-16 17:15pm    
still facing the same issue doing that as well. :/
Matt T Heffron 10-May-16 18:27pm    
OK, I'm confused.
I just tried this (in C#) in a new ASP.NET Web Application project.
I added the C# equivalent of your original code to the provided About.aspx page, in the Page_Load method.
It works fine and displays a 2 column grid with 1 row.
The column headings are the two string used.
I don't know what the problem is for you.
Use the debugger to see if you can get more information about the what's happening when you get the error.
JT1992 10-May-16 18:29pm    
Can you post the code that you tried in c#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900