Must declare scalar variable, vb.net
-
10-02-2021 - |
문제
I have been debugging my code for a while and looking at posts in other forums, but it seems to be that everyone has a different problem than mine and what works for them will not work for me.
My dropdown list is supposed to filter a gridview by choosing all the Companies that are associated to a certain product. The solution I have found online is that most people did not have DataKeyNames set in their gridview.
I do have this set, but not to CompanyID. It wouldn't make any sense. So I have no idea what I am supposed to do since this is the only answer I have found.
<asp:DropDownList ID="ddlCompany" runat="server" DataSourceID="dsCompanyFilter"
DataTextField="CompanyName" DataValueField="CompanyID" AppendDataBoundItems="true"
AutoPostBack="true">
Protected Sub ddlCompany_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ddlCompany.SelectedIndexChanged
Dim CompanyID As Integer = ddlCompany.SelectedValue
dsProductLookup.SelectCommand = "SELECT ProductName, CompanyID, CompanyName
FROM Product, Company
WHERE CompanyID = @CompanyID
ORDER BY ProductName"
dsProductLookup.SelectParameters.Add("@CompanyID", ddlCompany.SelectedValue)
End Sub
<asp:gridview id="gvProducts" runat="server" AutoGenerateColumns="false"
datakeynames="ProductID" datasourceid="dsProductLookup" style="margin-top: 12px;">
<asp:sqldatasource id="dsProductLookup" runat="server"
Connectionstring="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT ProductID, ProductName FROM [Product] ORDER BY [ProductName]">
</asp:sqldatasource>
<asp:SqlDataSource ID="dsCompanyFilter" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT [CompanyName], [CompanyID] FROM [Company] ORDER BY CompanyName">
</asp:SqlDataSource>
UPDATE: I tried adding this to another page in my site and have the same Declaration error. Must declare the scalar variable "@CompanyID".
Choose Company: <asp:DropDownList ID="ddlCompany" runat="server" DataSourceID="dsCompanyFilter" DataTextField="CompanyName" DataValueField="CompanyID" AppendDataBoundItems="true" AutoPostBack="true">
<asp:ListItem Enabled="true" Text="Select an option"></asp:ListItem>
</asp:DropDownList>
<asp:gridview id="gvCategories" runat="server" AutoGenerateColumns="False"
datakeynames="CategoryID,CompanyID" datasourceid="dsCategoryLookup" emptydatatext="No
categories/products found." style="margin-top: 12px;">
<Columns>
<asp:BoundField HeaderText="Category Name" DataField="CategoryName" />
<asp:HyperLinkField DataNavigateUrlFields="ProductID"
DataNavigateUrlFormatString="Product/Default.aspx?ID={0}"
DataTextField="ProductName"
HeaderText="Product Name" />
</Columns>
</asp:gridview>
<asp:SqlDataSource ID="dsCategoryLookup" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT DISTINCT c.CategoryName, c.CategoryID, p.ProductName,
p.ProductID, cl.CompanyID, cl.ProductID, co.CompanyID
FROM Category AS c
INNER JOIN CategoryLink AS l ON l.CategoryID = c.CategoryID
INNER JOIN Product AS p ON p.ProductID = l.ProductID
LEFT JOIN CompanyLink AS cl ON cl.ProductID = p.ProductID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsCompanyFilter" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT [CompanyName], [CompanyID]
FROM [Company]
ORDER BY CompanyName">
</asp:SqlDataSource>
Protected Sub ddlCompany_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ddlCompany.SelectedIndexChanged
dsCategoryLookup.SelectCommand = "SELECT Category.CategoryName,
CategoryLink.ProductID, Company.CompanyName,
Company.CompanyID, CompanyLink.CompanyID AS Expr1,
Product.ProductName, Product.ProductID,
CompanyLink.ProductID AS Expr2
FROM Company
LEFT JOIN CompanyLink
ON Company.CompanyID = CompanyLink.CompanyID
LEFT JOIN Product
ON CompanyLink.ProductID = Product.ProductID
LEFT JOIN CategoryLink
ON CategoryLink.ProductID = Product.ProductID
LEFT JOIN Category
ON CategoryLink.CategoryID = Category.CategoryID
WHERE Company.CompanyID = @CompanyID
ORDER BY Product.ProductName"
해결책 2
I was able to find an answer on the ASP.net forum that works with my site. (So far...)
Protected Sub ddlCompany_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ddlCompany.SelectedIndexChanged
dsProductLookup.SelectCommand = "SELECT Company.CompanyName, Company.CompanyID,
CompanyLink.CompanyID AS Expr1,
Product.ProductName, Product.ProductID,
CompanyLink.ProductID AS Expr2
FROM Company
INNER JOIN CompanyLink ON Company.CompanyID = CompanyLink.CompanyID
INNER JOIN Product ON CompanyLink.ProductID = Product.ProductID
WHERE Company.CompanyID = @CompanyID
ORDER BY Product.ProductName"
dsProductLookup.SelectParameters.Clear()
Dim controlParam As ControlParameter = New ControlParameter
controlParam.ControlID = "ddlCompany"
controlParam.DefaultValue = "-1"
controlParam.Name = "CompanyID"
controlParam.PropertyName = "SelectedValue"
controlParam.Type = TypeCode.Decimal
dsProductLookup.SelectParameters.Add(controlParam)
End Sub
다른 팁
Instead of setting the parameter in the code-behind, add a ControlParameter
in the markup for the datasource control.
<SelectParameters>
<asp:ControlParameter ControlID="ddlCompany" PropertyName="SelectedValue" DbType="Int32" DefaultValue="0" />
</SelectParameters>
Given your description, I think the above should solve your problem, but if not you shouldn't feel discouraged from adding CompanyID
as a data key. Just remember to include CompanyID
as a column in the select statement of dsProductLookup
.
EDIT
It looks like I omitted the parameter name in the previous answer. Try something like this instead:
<SelectParameters>
<asp:ControlParameter Name="CompanyID" ControlID="ddlCompany" PropertyName="SelectedValue" Type="Int32" DefaultValue="0" />
</SelectParameters>
Like I said, I don't use datasource controls, so I'm not sure if you have to prefix the parameter name with @
or not, but if you do:
<SelectParameters>
<asp:ControlParameter Name="@CompanyID" ControlID="ddlCompany" PropertyName="SelectedValue" Type="Int32" DefaultValue="0" />
</SelectParameters>