Ambiguous column name \'VendorID\'. How do I fix this error in the my code? Erro
ID: 3813223 • Letter: A
Question
Ambiguous column name 'VendorID'.
How do I fix this error in the my code? Error happens around the end.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Defaultpg2.aspx.cs" Inherits="AppDevTutoial.Defaultpg2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:IS315Database1ConnectionString %>" SelectCommand="SELECT * FROM [vendor]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="VendorName" DataValueField="VendorID" Height="36px" Width="125px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:IS315Database1ConnectionString %>" SelectCommand="SELECT [ProductName], [ProductPrice] FROM [Product] WHERE ([VendorID] = @VendorID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="VendorID" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3">
<Columns>
<asp:BoundField DataField="VendorName" HeaderText="VendorName" SortExpression="VendorName" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" SortExpression="ProductPrice" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:IS315Database1ConnectionString %>" SelectCommand="SELECT vendor.VendorName, Product.ProductName, Product.ProductPrice, Category.CategoryName FROM vendor INNER JOIN Product ON vendor.VendorID = Product.VendorID INNER JOIN Category ON Product.CategoryID = Category.CategoryID WHERE ([VendorID] = @VendorID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="VendorID" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Explanation / Answer
I think you have ambiguity only in VendorID. Other fields seem distinct. So try This
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:IS315Database1ConnectionString %>" SelectCommand="SELECT vendor.VendorName, Product.ProductName, Product.ProductPrice, Category.CategoryName FROM vendor INNER JOIN Product ON vendor.VendorID = Product.VendorID INNER JOIN Category ON Product.CategoryID = Category.CategoryID WHERE ([VendorID] = @VendorID)">
You have a sql query where you are find data with vendroID but VendorID is in two table
vendor.VendorID ,
Product.VendorID
This make sql difficult to point which VenkdorID should be selected.
I have make bold on the query where you have to change
WHERE ([VendorID] = @VendorID)">
Either to
WHERE (vendor.VendorID = @VendorID)"> or
WHERE (Product.VendorID = @VendorID)">
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.