【问题标题】:Populate ASP DataGrid With Fields With Spaces使用带空格的字段填充 ASP DataGrid
【发布时间】:2018-06-13 04:44:29
【问题描述】:

我有一个 SQL Server 表,其中包含带有空格的字段。我正在尝试使用此信息填充 asp 数据网格,但在 Bind() 事件中,我不断收到错误

附加信息:在所选数据源上找不到名为“[带空格的字段]”的字段或属性。

我知道该字段已正确命名,就好像我在 SSMS 中运行它一样,它可以正确执行。下面是我的 HTML 和我的 C# - 什么设置不正确?

using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LSTMain"].ConnectionString))
{
    cn.Open();
    SqlCommand cmd = new SqlCommand("SELECT [Field With Space] FROM [Inventory Log] order by ID ASC", cn);
    SqlDataReader dr = cmd.ExecuteReader();
    GridView1.DataSource = dr;
    GridView1.DataBind();
    cn.Close();
}


<div id="dgv">
<asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="[Field With Space]" HeaderText="SQL Field With Spaces" />
    </Columns>
</asp:GridView>

【问题讨论】:

  • 所以DataField 可能不应该包含空格。
  • @S.Akbari - 所以我需要在我的 SQL 表中用空格重命名字段?
  • @S.Akbari - 从 SQL Server 表字段名称中删除空格似乎可以补救。只是令人讨厌,这是实现这一目标的唯一方法。

标签: c# asp.net datagrid webforms


【解决方案1】:

您可以使用数据阅读器 dr.GetString(0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2017-04-20
    • 1970-01-01
    • 2013-02-20
    • 2012-07-21
    相关资源
    最近更新 更多