TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Abhijit Chougule
NA
12
12.5k
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.
Reply
Answers (
1
)
How to create API Key Registration in google map controls
Bind a Status Column in gridview on basis current Date