绑定级联下拉列表时出错

本文关键字:出错 下拉列表 级联 绑定 | 更新日期: 2023-09-27 18:30:01

当我移动到c#应用程序中的另一个页面时,我面临一条错误消息,该页面包含级联下拉列表。我不知道该怎么修,所以你能帮我修吗?

参数化查询"(@Country nvarchar(4000))SELECT State FROM State WHERE Country="需要参数"@Country",但未提供该参数。

protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies.Get("Location");

        if (!IsPostBack)
        {
            if (cookie != null)
            {
                DataTable StateDT = new DataTable();
                using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
                {
                    // Create the SelectCommand.
                    SqlCommand command = new SqlCommand("SELECT State FROM State WHERE Country = @Country", con2);
                    command.Parameters.AddWithValue("@Country", (cookie["Location"]));
                    SqlDataAdapter adaptar = new SqlDataAdapter();
                    adaptar.SelectCommand = command;
                    adaptar.Fill(StateDT);
                    carstatedrdolst.DataSource = StateDT;
                    carstatedrdolst.DataTextField = "State";
                    carstatedrdolst.DataBind();
                }
                carstatedrdolst.Items.Insert(0, new ListItem("Select State", ""));
            }
        }

}

绑定级联下拉列表时出错

我认为您需要获得类似的cookie值

HttpCookie cookie = Request.Cookies["Location"];
if(cookie != null) {
     object countryName= cookie.Value;
} else {
     //Handle the null
}

请检查您通过的cookie值。或者出于测试目的,您可以手动对Country进行硬编码。

string country = "Japan";

并将该值传递给命令参数

command.Parameters.AddWithValue("@Country", country);