Error with binding my cascading dropdownlist

69 views Asked by At

I am facing an error message when move to another page in my c# application which this page contain cascading dropdownlist. I didnt know how i can fix it so could you please help me with it.

The parameterized query '(@Country nvarchar(4000))SELECT State FROM State WHERE Country =' expects the parameter '@Country', which was not supplied.

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

There are 2 answers

0
Dnyanesh On BEST ANSWER

I think you need to get values of cookie like

HttpCookie cookie = Request.Cookies["Location"];

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

Kindly check the cookie value that you pass. Or for testing purposes, you can manually hardcoded the Country something like.

string country = "Japan";

and pass the value to the command parameter

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