Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

c# - Ambiguous column name 'ProductID' in asp.net

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

enter image description here

UPDATE:

I get this error:

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...