【问题标题】:A field or property with the name 'Id' was not found on the selected data source在所选数据源上找不到名为“Id”的字段或属性
【发布时间】:2016-10-23 18:35:04
【问题描述】:

我不确定我在这里缺少什么?我刚刚更改了我的 sql 语句,现在它说它没有读取“ID”,有人可以帮我吗?

这是我的 html 代码:

<asp:GridView ID="gvCustomerOrders" runat="server" Width="940px"  HorizontalAlign="Center"
    AutoGenerateColumns="false"   AllowPaging="True" CssClass="table table-hover table-striped" OnPageIndexChanging="gvCustomerOrders_PageIndexChanging">

<Columns>
                    <asp:BoundField DataField ="Id" HeaderText ="Id" ItemStyle-Width="100" >
                    <ItemStyle Width="100px"></ItemStyle>
                    </asp:BoundField>
                    <asp:BoundField DataField ="CustomerName" HeaderText ="Name" />    
                    <asp:BoundField DataField ="CustomerPhoneNo" DataFormatString= "{0:(###) ###-####}" HeaderText ="PhoneNo" />
                    <asp:BoundField DataField ="CustomerEmailID" HeaderText ="Email" />
                    <asp:BoundField DataField ="Name" HeaderText ="Product" />
                    <asp:BoundField DataField ="TotalPrice" DataFormatString="{0:C2}" HeaderText ="Price" />


                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ='<%# Eval("Id", "~/Admin/OrderDetails.aspx?Id={0}") %>'
                                Text="View Details" Target="_blank" />
                        </ItemTemplate>
                        </asp:TemplateField>
</Columns>

和后面的代码:

public void bindgrid()
    {
        SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
        SqlCommand cmd = new SqlCommand("select DISTINCT STUFF((SELECT ',' + p.[name] FROM  Products p INNER JOIN CustomerProducts cp ON cp.ProductID = p.ProductID WHERE cp.CustomerID = cd.Id FOR XML PATH(''),TYPE).value('.','nvarchar(max)'),1,1,'') AS Name, cd.CustomerName, cd.CustomerEmailID ,cd.CustomerPhoneNo,cd.CustomerAddress ,cd.TotalPrice,cd.OrderDateTime, cd.PaymentMethod FROM CustomerDetails cd Inner Join CustomerProducts cp ON cp.CustomerID = cd.Id", conn);

        SqlDataAdapter da = new SqlDataAdapter("", conn);
        da.SelectCommand = new SqlCommand("select DISTINCT STUFF((SELECT ',' + p.[name] FROM  Products p INNER JOIN CustomerProducts cp ON cp.ProductID = p.ProductID WHERE cp.CustomerID = cd.Id FOR XML PATH(''),TYPE).value('.','nvarchar(max)'),1,1,'') AS Name, cd.CustomerName, cd.CustomerEmailID ,cd.CustomerPhoneNo,cd.CustomerAddress ,cd.TotalPrice,cd.OrderDateTime, cd.PaymentMethod FROM CustomerDetails cd Inner Join CustomerProducts cp ON cp.CustomerID = cd.Id", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");
        gvCustomerOrders.DataSource = ds.Tables[0].DefaultView;
        gvCustomerOrders.DataBind();
    }

真的不知道为什么它给了我错误

A field or property with the name 'Id' was not found on the selected data source.

【问题讨论】:

    标签: html sql asp.net boundfield


    【解决方案1】:

    您的查询:

    select DISTINCT STUFF((SELECT ',' + p.[name] FROM  Products p INNER JOIN CustomerProducts cp ON cp.ProductID = p.ProductID WHERE cp.CustomerID = cd.Id FOR XML PATH(''),TYPE).value('.','nvarchar(max)'),1,1,'') AS Name, cd.CustomerName, cd.CustomerEmailID ,cd.CustomerPhoneNo,cd.CustomerAddress ,cd.TotalPrice,cd.OrderDateTime, cd.PaymentMethod FROM CustomerDetails cd Inner Join CustomerProducts cp ON cp.CustomerID = cd.Id
    

    不返回名为 'Id' 的列

    但您在此处绑定Id 列:

    <asp:BoundField DataField ="Id" HeaderText ="Id" ItemStyle-Width="100" >
    

    我猜你只需要在查询中包含客户表中的“Id”列:

    select DISTINCT Id, STUFF((......
    

    【讨论】:

      【解决方案2】:

      看到您有一个&lt;asp:BoundField../&gt;DataField ="Id",并且您正在使用的查询不会获取任何名称为"Id 的列,如果您获取该列,则意味着此错误将得到解决。或者如果在这种情况下不需要 BoundField,则从前端删除它。

      注意:-您应该在绑定集合中提供所有指定的DataFields,这意味着您应该通过查询获取这些列

      【讨论】:

        猜你喜欢
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 2015-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多