必须声明标量变量vb.net

时间:2011-10-21 20:59:01

标签: asp.net vb.net declaration scalar

我一直在调试我的代码并查看其他论坛的帖子,但似乎每个人都有一个与我不同的问题,对他们有用的东西对我不起作用。

我的下拉列表应该通过选择与特定产品相关联的所有公司来过滤网格视图。我在网上找到的解决方案是大多数人没有在gridview中设置DataKeyNames。

我确实有这套,但不是公司ID。这没有任何意义。所以我不知道我应该做什么,因为这是我找到的唯一答案。

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

更新:我尝试将其添加到我网站的其他网页,并发出相同的声明错误。 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 个答案:

答案 0 :(得分:0)

不是在代码隐藏中设置参数,而是在数据源控件的标记中添加ControlParameter

<SelectParameters>
    <asp:ControlParameter ControlID="ddlCompany" PropertyName="SelectedValue" DbType="Int32" DefaultValue="0" />
</SelectParameters>

鉴于您的描述,我认为以上内容应该可以解决您的问题,但如果不是,您不应该因为添加CompanyID作为数据密钥而感到沮丧。请记住在CompanyID的选择语句中包含dsProductLookup列。

修改

看起来我在上一个答案中省略了参数名称。尝试这样的事情:

<SelectParameters>
    <asp:ControlParameter Name="CompanyID" ControlID="ddlCompany" PropertyName="SelectedValue" Type="Int32" DefaultValue="0" />
</SelectParameters>

就像我说的,我不使用数据源控件,所以我不确定你是否必须在@之前加上参数名称的前缀,但如果你这样做:

<SelectParameters>
    <asp:ControlParameter Name="@CompanyID" ControlID="ddlCompany" PropertyName="SelectedValue" Type="Int32" DefaultValue="0" />
</SelectParameters>

答案 1 :(得分:0)

我能够在与我的网站一起使用的ASP.net论坛上找到答案。 (到目前为止......)

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