Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how can i make Data Binding in combobox from sql server query (
SQL
select id , name from town where countryid = combobox2.selectedvalue
)
and display member = name
and value member = id
and please write a view of code
thanks for our effort
best regards
Posted
Comments
Kornfeld Eliyahu Peter 8-Mar-15 5:14am    
Beside the mess in your question, What have you tried?[^]
Mohamed-Hmy 8-Mar-15 6:06am    
I Tried To Make Data Binding in combo box based on another combo box
from Sql Query
question about ... how can i do this using C# Windows application
i am beginner in c# windows application
best regards

1 solution

SqlConnection con=new SqlConnection("your_connection_string");
SqlCommand cmd=new SqlCommand();
SqlDataAdapter ada=new SqlDataAdapter();
DataTable dt=new DataTable();

/* Write this code inside the first combobox click event */
try{
cmd.Connection=con;
cmd.CommandText="select id , name from town where countryid ="+ combobox1.selectedvalue;
ada.SelectCommand=cmd;
ada.Fill(dt);
if(dt.Rows.Count>0)
{
combobox2.DataSource=dt;
combobox2.DisplayMember="name";
combobox2.ValueMember="id";
}
else
{

}
}
catch(Exception exp)
{

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900