繁体   English   中英

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

[英]ASP.NET Populate Webforms Listbox based on selection from other Listbox and Database Values + declare scalar variable error

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

我收到错误消息:必须使用现有代码声明标量变量@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();

    }

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

 up_SelectPriceFinderResults

由于我看不到存储过程的源代码,因此我假设您忘记了声明参数或变量。 您必须在存储过程中声明变量@state_code:

 declare @state_code varchar(3)

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

exec sp_helptext up_SelectPriceFinderResults

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM