ASP.NET根据其他列表框和数据库值的选择填充Webforms列表框+声明标量变量错误

时间:2011-02-22 17:16:35

标签: c# asp.net listbox webforms

一旦根据数据库的state_zipcode_plans表中定义的值从另一个列表框中选择状态,我希望列表框自动填充邮政编码。 EX:如果选择了FL的状态,请将zip_codes(332,333,334,335)加载到列表框中,其中state_code = selected_state_code(FL)

我收到错误:必须使用现有代码声明标量变量@statecode。

我在想也许我应该把Request.Form(“state_code”)放在某处? 我的朋友建议在CS代码中添加一行,但我不确定语法。 我如何使其工作?

这是我的代码:

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

                                                                     

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

这是C#中的代码隐藏:

    protected void Search_STZipPlan_Button_Click(object sender, EventArgs e)
    {

        SqlConnection conn = new SqlConnection("Server=localhost;Database=PriceFinder;Integrated Security=SSPI");
        conn.Open();

        SqlCommand searchPF = new SqlCommand("up_SelectPriceFinderResults", conn); //calling stored procedure 
        searchPF.CommandType = CommandType.StoredProcedure;


        SqlConnectionStringBuilder builder =  new SqlConnectionStringBuilder();
        builder["Data Source"] = "SqlDataSource1";
        builder["integrated Security"] = true;
        builder["Initial Catalog"] = "PriceFinder;NewValue=Bad";
        Console.WriteLine(builder.ConnectionString);

        SqlParameter state_code = new SqlParameter("state_code", SqlDbType.VarChar, 3); 
        SqlParameter zip_code = new SqlParameter("zip_code", SqlDbType.VarChar, 6); 
        SqlParameter plan_name = new SqlParameter("plan_name", SqlDbType.VarChar, 16);

        // SqlDataReader reader = SqlCommand.ExecuteReader();
        // Search_Results_GridView.DataSource = reader;

        Search_Results_GridView.DataBind();

        conn.Close();

    }

1 个答案:

答案 0 :(得分:0)

问题与SQL有关。存储过程中可能存在错误

 up_SelectPriceFinderResults

由于我没有看到存储过程的源代码,我认为您忘记声明参数或变量。您必须在存储过程中声明变量@state_code:

 declare @state_code varchar(3)

或作为存储过程的参数。无论如何,检查存储过程的源代码:

exec sp_helptext up_SelectPriceFinderResults