Abhijit Chougule

Abhijit Chougule

  • NA
  • 12
  • 12.6k

asp.net cascading dropdownlist databinding

Mar 11 2015 12:43 AM
I am trying to bind a country and state dropdownlist using sql serever with foreign key. i have written the code in country  dropdown list selected index changed as
 
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
ddlstate.Items.Clear();
ddlstate.Items.Add(new ListItem("--Select State--", ""));
ddlstate.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings[ "CoffeeConnection" ].ConnectionString;
String strQuery = "select stateid,statename from userstate " + "where stid=@stid" ;
SqlConnection con = new SqlConnection (strConnString);
SqlCommand cmd = new SqlCommand ();
cmd.Parameters.AddWithValue( "@stid" ,ddlcountry.SelectedItem.Value);
cmd.CommandType = CommandType .Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlstate.DataSource = cmd.ExecuteReader();
ddlstate.DataTextField = "countryname" ;
ddlstate.DataValueField = "countryid" ;
ddlstate.DataBind();
if (ddlstate.Items.Count > 1)
{
ddlstate.Enabled = true ;
}
else
{
ddlstate.Enabled = false ;

}
}
catch ( Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
 
 
 
 
It gives the  error as
Conversion failed when converting the nvarchar value 'India' to data type int.  

Answers (1)