Ambiguous column name 'ProductID' in asp.net

a 夏天 提交于 2019-12-14 03:35:31

问题


I have this in my html:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand = "GridView1_RowCommand"
        DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
                SortExpression="CustomerID" />
        <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                SortExpression="ProductID" />
        <asp:BoundField DataField="TotalProduct" HeaderText="TotalProduct" 
                SortExpression="TotalProduct" />
        <asp:BoundField DataField="UpdatedProduct" HeaderText="UpdatedProduct" SortExpression="UpdatedProduct" />
        <asp:BoundField DataField="ProductQuantity" HeaderText="ProductQuantity" SortExpression="ProductQuantity" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%# Eval("ProductID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Myconn %>" 
        SelectCommand="SELECT CustomerProducts.*, Products.ProductQuantity FROM CustomerProducts INNER JOIN Products ON CustomerProducts.ProductID = Products.ProductID">
</asp:SqlDataSource>

and here is the code behind:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "Approve")
        {
            using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
            {
                scn.Open();
                SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID WHERE ProductID=@ProductID", scn);
                cmd.Parameters.AddWithValue("@ProductID", ID);
                cmd.ExecuteNonQuery();
            }
        }
    }

I really don't have an idea what is the error I'm getting. What I'm trying to do here is to upon link click, it will update the updatedproduct column.

Here is a screenshot

UPDATE:

I get this error:

Conversion failed when converting the nvarchar value '__Page' to data type int.


回答1:


Since the column ProductID is present in both tables, the WHERE clause find it Ambiguous. So,

Replace ProductID=@ProductID with o.ProductID=@ProductID

update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct 
from CustomerProducts o 
inner join Products p 
on o.ProductID = p.ProductID WHERE o.ProductID=@ProductID


来源:https://stackoverflow.com/questions/38029650/ambiguous-column-name-productid-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!