locked
Auto-populated Listbox based on selection from other Listbox RRS feed

  • Question

  • User816364259 posted

    I'm trying to auto-populate a listbox of zip codes from a database table state_zipcode_plans based on the state a user selects from the previous listbox.  I'm getting the error: Must declare the scalar variable @state_code.
    Any suggestions on fixing this?
    Can it be fixed by adding in Request.Form("state_code"

    ) in somewhere?

     

    Here's my code:

     

     

    <asp:ListBox ID="ST_Select_ListBox" runat="server" DataSourceID="SqlDataSource1"

     

    DataTextField="state_code" DataValueField="state_code" CssClass="style7"></asp:ListBox

    >

     

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"

     

    ConnectionString="<%$ ConnectionStrings:PriceFinderConnectionString %>"

     

    SelectCommand

    ="SELECT [state_code] FROM [state_code] ORDER BY [state_code]">

     

    <SelectParameters

    >

     

    <asp:FormParameter DefaultValue="NULL" FormField="state_code" Name="state_code" Type="String"

    />

     

    </SelectParameters

    >

     

    </

     

    asp:SqlDataSource

    >

     

    <br class="style7"

    />

     

    </td

    >

     

    <td class="style2"> <span class="style7">

    Select Zip (auto-populated based on selected State)

     

    </span

    >

     

    <br class="style7"

    />

     

    <br class="style7"

    />

     

    <asp:ListBox ID="Zip_Select_ListBox" runat="server" DataSourceID="SqlDataSource2" CssClass

    ="style7">

     

    </asp:ListBox

    >

     

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"

     

    ConnectionString="<%$ ConnectionStrings:PriceFinderConnectionString %>"

     

    SelectCommand="SELECT DISTINCT [zip_code] FROM [state_zipcode_plans] WHERE ([state_code] = @state_code)" Request.Form("state_code")

    >

     

    <SelectParameters

    >

     

    <asp:FormParameter DefaultValue="NULL" FormField="zip_code" Name="zip_code" Type="String"

    />

     

    </SelectParameters

    >

     

    </asp:SqlDataSource

    >

     

     

     

     

     

    Tuesday, February 22, 2011 12:02 PM

Answers

  • User1224194097 posted

    Whenever you define parameters in SelectCommand, parameters should be defined in the SelectParameters as well..

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT DISTINCT [zip_code] FROM [state_zipcode_plans] WHERE ([state_code] = @state_code)">
        <SelectParameters>
            <asp:ControlParameter ControlID="ST_Select_ListBox" Name="state_code" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 22, 2011 12:29 PM

All replies

  • User1224194097 posted

    Whenever you define parameters in SelectCommand, parameters should be defined in the SelectParameters as well..

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT DISTINCT [zip_code] FROM [state_zipcode_plans] WHERE ([state_code] = @state_code)">
        <SelectParameters>
            <asp:ControlParameter ControlID="ST_Select_ListBox" Name="state_code" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 22, 2011 12:29 PM
  • User816364259 posted

    how is that different from what i had before?

    Monday, March 7, 2011 11:08 AM
  • User1224194097 posted

    Only difference is adding the select parameter for the SqlDataSource to pass the State ID from the DropDownlist selection to filter Districts.

    Monday, March 7, 2011 1:09 PM