ASP.NET错误,必须在选择第二个下拉列表后声明标量变量“@ServerName”

时间:2021-10-31 10:12:25

There are 3 dropdownlists which are parent-child, after the 3rd dropdownlist is selected, the DetailsView will display result from all 3 dropdownlist selected. first is ServerName, second is Instance, then third is DatabaseName. After select first dropdownlist of servername, then new list of instance value appear on second dropdownlist. When I select anything on second dropdownlist. There a error message that say, "Must declare the scalar variable "@ServerName"". I don't understand what it mean and please help. Here a dropdownlist codes,

有3个下拉列表是父子,在选择第3个下拉列表后,DetailsView将显示所有3个下拉列表中的结果。第一个是ServerName,第二个是Instance,第三个是DatabaseName。选择servername的第一个下拉列表后,第二个下拉列表中会出现新的实例值列表。当我在第二个下拉列表中选择任何内容时。有一条错误消息说“必须声明标量变量”@ServerName“”。我不明白它的意思,请帮忙。这里有一个下拉列表代码,

<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend" AutoPostBack="True" DataSourceID="SqlDataSource4" DataTextField="ServerName" DataValueField="ServerName" DropDownStyle="Simple" MaxLength="0" style="display: inline;">
</ajaxToolkit:ComboBox>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ServerName] FROM [tblServer]">
</asp:SqlDataSource>

<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = @ServerName">
    <SelectParameters>
        <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = @ServerName AND [Instance] = @Instance">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

Then I pass those dropdownlist function to DetailsView that come with SQLDataSource2, on SelectCommand in SQLDataSource2, I wrote SelectCommand="SELECT * FROM [tblDatabase] WHERE (([DatabaseName] = @DatabaseName) AND ([Instance] = @Instance) AND ([ServerName] = @ServerName))"

然后我将这些dropdownlist函数传递给SQLDataSource2附带的DetailsView,在SQLDataSource2的SelectCommand上,我写了SelectCommand =“SELECT * FROM [tblDatabase] WHERE(([DatabaseName] = @DatabaseName)AND([Instance] = @Instance)AND( [ServerName] = @ServerName))“

Then after that I add SelectParameter codes,

然后我添加SelectParameter代码,

<SelectParameters>
    <asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" Type="String" />
    <asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" Type="String" />
    <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>

2 个解决方案

#1


1  

In SqlDataSource3, your query takes two parameters: @ServerName and @Instance. But in the select parameters, you only define @Instance. Yes, you defined @ServerName in the previous data source, but not in this one.

在SqlDataSource3中,您的查询有两个参数:@ServerName和@Instance。但在select参数中,您只定义@Instance。是的,您在先前的数据源中定义了@ServerName,但未在此数据源中定义。

#2


0  

I fixed from what Jay metioned recently and there also another issue that I fix it on instance and database dropdownlist codes, where what I fix and it work fine.

我修正了Jay最近提到的问题,还有另一个问题,我在实例和数据库下拉列表代码上修复它,我修复了它并且它工作正常。

<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
    <br />
    <asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
    </asp:DropDownList>
    <asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = @ServerName">
        <SelectParameters>
            <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>

    <br />
    <br />
    <asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
    <br />
    <asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = @ServerName AND [Instance] = @Instance">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>

#1


1  

In SqlDataSource3, your query takes two parameters: @ServerName and @Instance. But in the select parameters, you only define @Instance. Yes, you defined @ServerName in the previous data source, but not in this one.

在SqlDataSource3中,您的查询有两个参数:@ServerName和@Instance。但在select参数中,您只定义@Instance。是的,您在先前的数据源中定义了@ServerName,但未在此数据源中定义。

#2


0  

I fixed from what Jay metioned recently and there also another issue that I fix it on instance and database dropdownlist codes, where what I fix and it work fine.

我修正了Jay最近提到的问题,还有另一个问题,我在实例和数据库下拉列表代码上修复它,我修复了它并且它工作正常。

<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
    <br />
    <asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
    </asp:DropDownList>
    <asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = @ServerName">
        <SelectParameters>
            <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>

    <br />
    <br />
    <asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
    <br />
    <asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = @ServerName AND [Instance] = @Instance">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>