【问题标题】:Ambiguous column name 'ProductID' in asp.netasp.net 中不明确的列名“ProductID”
【发布时间】:2016-06-25 14:44:19
【问题描述】:

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

下面是代码:

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

我真的不知道我遇到了什么错误。我在这里尝试做的是点击链接,它将更新updatedproduct 列。

这是截图

更新:

我收到此错误:

将 nvarchar 值“__Page”转换为数据类型 int 时转换失败。

【问题讨论】:

  • 8 小时 5 个问题,所有这些都是真正的基本 sql 概念。请学习sql教程。这不是一个教程网站。而且您使用的是 Microsoft SQL Server,而不是我们在昨晚之后知道的 mysql。

标签: c# asp.net sql-server gridview asplinkbutton


【解决方案1】:

由于ProductID 列在两个表中都存在,WHERE 子句发现它不明确。所以,

ProductID=@ProductID 替换为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

【讨论】:

  • @ReineViray 我在您问题的代码中看不到__Page
  • @ReineViray 你在数据库中有更新触发器吗?
  • 触发先生是什么意思?实际上,当我使用按钮尝试时,我现在拥有的语句运行良好,但问题是它更新了所有updatedproduct column,而我想要的只是在链接单击先生后更新单个列。谢谢
  • @ReineViray 变量“ID”的值为__Page,它应该是整数。你从哪里读到这篇文章?
  • @Riene 这是正确答案。现在你应该可以工作了。
猜你喜欢
  • 1970-01-01
  • 2014-05-03
  • 2018-01-21
  • 2020-11-28
  • 2023-03-08
  • 1970-01-01
  • 2017-04-09
  • 2018-10-23
  • 1970-01-01
相关资源
最近更新 更多