Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
1
Answer
asp.net cascading dropdownlist databinding
Abhijit Chougule
9y
497
1
Reply
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.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
How to create API Key Registration in google map controls
Bind a Status Column in gridview on basis current Date