C# İl-İlçe Örneği(SQL Server ile) için tıklayınız.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace iller
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection cnn = new SqlConnection(“Data Source =.\\SQLEXPRESS; Initial Catalog = iller; Integrated Security = true;”);
public DataSet doldur(string sorgu)
{
cnn.Open();
SqlDataAdapter adp = new SqlDataAdapter(sorgu,cnn);
DataSet ds = new DataSet();
adp.Fill(ds);
cnn.Close();
return ds;
}
private void Form1_Load(object sender, EventArgs e)
{
string sorgu = “select ilAdi from il order by ilAdi”;
DataSet ds = doldur(sorgu);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
string sorgu = “select Plaka from il where ilAdi='”+comboBox1.Text+”‘ “;
DataSet ds = doldur(sorgu);
sorgu = “select ilceAdi from ilce where Plaka='”+ds.Tables[0].Rows[0][0]+”‘”;
ds.Clear();
ds = doldur(sorgu);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox2.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
}
}
}