绑定我的级联下拉列表时出错

时间:2014-11-12 02:21:42

标签: c# asp.net cookies cascading dropdownlistfor

当我移动到我的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", ""));
            }
        }

}

2 个答案:

答案 0 :(得分:0)

请检查您传递的Cookie值。或者出于测试目的,您可以手动硬编码国家/地区。

string country = "Japan";

并将值传递给命令参数

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

答案 1 :(得分:0)

我认为你需要获得像

这样的cookie值
HttpCookie cookie = Request.Cookies["Location"];

if(cookie != null) {
     object countryName= cookie.Value;
} else {
     //Handle the null
}