asp.net 中的列名不明确 'ProductID'
Ambiguous column name 'ProductID' in asp.net
我的 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();
}
}
}
我真的不知道我收到的错误是什么。我在这里要做的是点击 link 后,它会更新 updatedproduct
列。
这是截图
更新:
我收到这个错误:
Conversion failed when converting the nvarchar value '__Page' to data type int.
由于列 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
我的 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();
}
}
}
我真的不知道我收到的错误是什么。我在这里要做的是点击 link 后,它会更新 updatedproduct
列。
这是截图
更新:
我收到这个错误:
Conversion failed when converting the nvarchar value '__Page' to data type int.
由于列 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