ASP.NET Populate Webforms Listbox based on selection from other Listbox and Database Values + declare scalar variable error
I want a listbox to auto-populate zip codes once a state is selected from another listbox based on values defined in the state_zipcode_plans table from the database. EX: If the state of FL is selected, load zip_codes (332, 333, 334, 335) where state_code=selected_state_code (FL) into the listbox
I get the error: Must declare the scalar variable @statecode with the existing code.
I was thinking maybe I should put Request.Form("state_code") in there somewhere? My friend suggested adding a line in the CS code behind but I'm not sure about the syntax. How do I make it work?
Here's the code I have:
<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>
Here's the codebehind in C#:
protected void Search_STZipPlan_Button_Click(object sender, EventArgs e)
{
SqlC开发者_StackOverflow社区onnection 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();
}
The problem is SQL related. There probably is a mistake in the stored procedure
up_SelectPriceFinderResults
Since I don't See the stored procedure's source code, I presume you forgot to declare a parameter or a variable. You'll have to declare the variable @state_code inside your stored procedure:
declare @state_code varchar(3)
or as a parameter of the stored procedure. Anyway, check the source code of the stored procedure:
exec sp_helptext up_SelectPriceFinderResults
精彩评论