htmlsqlasp.netboundfield

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


i am not sure what am i missing here? i just changed my sql statement and now it says that its not reading the "ID" can someone please help me out?

here is my html code:

<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>

and code behind:

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

really lost on this on why its giving me the error

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

Solution

  • Your query:

    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
    

    doesn't return a column named 'Id'

    but you're binding Id column here:

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

    I'm guessing you just need to include the 'Id' column from the customer table in your query:

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