0% found this document useful (0 votes)
10 views3 pages

Lect Combo Box Multi DB Updated

Combobox visual programming

Uploaded by

xxareefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Lect Combo Box Multi DB Updated

Combobox visual programming

Uploaded by

xxareefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Visual Programming

C# GUI
ADO.NET
Combo Box DB
Windows Form Control

Course:
Visual Programming

By:
Zohair Ahmed

www.youtube.com/@zohairahmed007 Subscribe
www.begindiscovery.com
ADO.NET COMBO BOX DB VISUAL PROGRAMMING

private void brn_load_Click(object sender, EventArgs e)


{
LoadProvinces();
}

private void LoadProvinces()


{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT ProvinceID, ProvinceName FROM tbl_Provinces", conn);
DataTable dt = new DataTable();
da.Fill(dt);
cbProvince.DisplayMember = "ProvinceName";
cbProvince.ValueMember = "ProvinceID";
cbProvince.DataSource = dt;
}
}

private void LoadCities(int provinceId)

Zohair Ahmed PAGE 2 OF 3


www.youtube.com/@zohairahmed007
www.begindiscovery.com
ADO.NET COMBO BOX DB VISUAL PROGRAMMING

private void LoadCities(int provinceId)


{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT CityID, CityName FROM tbl_Cities WHERE ProvinceID = @ProvinceID", conn);

da.SelectCommand.Parameters.AddWithValue("@ProvinceID", provinceId);
DataTable dt = new DataTable();
da.Fill(dt);
cbCity.DisplayMember = "CityName";
cbCity.ValueMember = "CityID";
cbCity.DataSource = dt;
}
}

private void cbProvince_SelectedIndexChanged(object sender, EventArgs e)


{
if (cbProvince.SelectedValue != null)
{
int provinceId = (int)cbProvince.SelectedValue;
LoadCities(provinceId);
}
}

Output

Zohair Ahmed PAGE 3 OF 3


www.youtube.com/@zohairahmed007
www.begindiscovery.com

You might also like