سلام.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("ConnectionString"))
using (SqlDataAdapter sda = new SqlDataAdapter(new SqlCommand()))
{
sda.SelectCommand.CommandText = "select City +' - ' + Country AS Address FROM Table1 WHERE id = " + comboBox1.SelectedItem + "";
sda.SelectCommand.Connection = con;
con.Open();
sda.SelectCommand.ExecuteNonQuery();
con.Close();
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox2.DataSource = dt;
comboBox2.DisplayMember = "Address";
}
}
روش دوم
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("ConnectionString"))
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select City +' - ' + Country AS Address FROM Table1 WHERE id = " + comboBox1.SelectedItem + "";
cmd.Connection = con;
con.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
comboBox2.Items.Add(dr["Address"].ToString());
}
}
con.Close();
}
}
http://stackoverflow.com/questions/17960283/how-to-get-data-from-sql-database-to-store-in-combo-box-c-sharp