asp.net中不明确的列名'ProductID' [英] Ambiguous column name 'ProductID' in asp.net

查看:561
本文介绍了asp.net中不明确的列名'ProductID'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseOnRowCommand =GridView1_RowCommand
DataSourceID =SqlDataSource1>
<列>
< asp:BoundField DataField =CustomerIDHeaderText =CustomerID
SortExpression =CustomerID/>
< asp:BoundField DataField =ProductIDHeaderText =ProductID
SortExpression =ProductID/>
< asp:BoundField DataField =TotalProductHeaderText =TotalProduct
SortExpression =TotalProduct/>
< asp:BoundField DataField =UpdatedProductHeaderText =UpdatedProductSortExpression =UpdatedProduct/>
< asp:BoundField DataField =ProductQuantityHeaderText =ProductQuantitySortExpression =ProductQuantity/>
< asp:TemplateField>
< ItemTemplate>
< asp:LinkBut​​ton ID =btnApproverunat =serverText =ApproveCommandName =ApproveCommandArgument ='<%#Eval(ProductID)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

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

以下是后面的代码:

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

我真的没有一个想法我得到了什么错误。我在这里要做的是在点击链接时,它会更新 updatedproduct 列。



是截图





更新:

>

我得到这个错误:


将nvarchar值'__Page'转换为数据类型为int。


ProductID 存在于两个表中,所以 WHERE 子句发现它含糊不清。因此,使用 o.ProductID=@ProductID 替换 ProductID = @ ProductID code

$ pre $ p $ update $ o更新o.Updatedproduct = p.ProductQuantity - o.Totalproduct
from CustomerProducts o
内连接产品p
on o.ProductID = p.ProductID WHERE o.ProductID=@ProductID


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.

解决方案

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

这篇关于asp.net中不明确的列名'ProductID'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆