C# 绑定我的级联下拉列表时出错
当我移动到我的c应用程序中的另一个页面(该页面包含级联dropdownlist)时,我面临一条错误消息。我不知道怎样才能修好它,所以你能帮我修一下吗 参数化查询'@Country nvarch4000select State FROM State WHERE Country='需要未提供的参数'@Country'C# 绑定我的级联下拉列表时出错,c#,asp.net,cookies,cascading,dropdownlistfor,C#,Asp.net,Cookies,Cascading,Dropdownlistfor,当我移动到我的c应用程序中的另一个页面(该页面包含级联dropdownlist)时,我面临一条错误消息。我不知道怎样才能修好它,所以你能帮我修一下吗 参数化查询'@Country nvarch4000select State FROM State WHERE Country='需要未提供的参数'@Country' protected void Page_Load(object sender, EventArgs e) { HttpCookie cookie = Reque
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值。或者出于测试目的,您可以手动对国家/地区进行硬编码,例如
string country = "Japan";
并将值传递给命令参数
command.Parameters.AddWithValue("@Country", country);
我认为你需要像这样获得cookie的值
HttpCookie cookie = Request.Cookies["Location"];
if(cookie != null) {
object countryName= cookie.Value;
} else {
//Handle the null
}
点击页面时,您是否检查了cookie[Location]中是否有价值?@Dnyanesh感谢您的回复,是的,因为我的应用程序中的列表视图根据cookie的价值显示了产品感谢您的支持,但这不起作用,因为从第一个网页获取的cookie将出现,用户可以从下拉列表中选择他的国家,并且我的应用程序中有许多国家,这是出于测试目的,这意味着该页面需要给定的国家。请将cookie的检查更改为if cookie!=如果cookie[位置]!=无效的