Level three linkage under the same table:
Code:
Class DBconnection
{
Public Const string CONNECTIONSTRING = "server=.; database=mydb;uid=sa;pwd=5587725 ";
}
Class Chinastates
{
public string AreaCode {get; set;}
public string AreaName {get;set;}
public string Parentareacode {get; set;}
}
Class CHIANSTATESDA
{
Private SqlConnection _conn;
Private SqlCommand _cmd;
Private SqlDataReader _dr;
Public Chianstatesda ()
{
_conn = new SqlConnection (dbconnection.connectionstring);
_cmd = _conn.createcommand ();
}
Public list<chinastates> Select (string parentareacode)
{
list<chinastates> list = new list<chinastates> ();
_cmd.commandtext = "SELECT * from chinastates where [email protected]";
_cmd.parameters.clear ();
_cmd.parameters.addwithvalue ("@parentareacode", Parentareacode);
Try
{
_conn.open ();
_DR = _cmd.executereader ();
while (_DR. Read ())
{
Chinastates data = new Chinastates ();
Data. AreaCode = _dr["AreaCode"]. ToString ();
Data. AreaName = _dr["AreaName"]. ToString ();
Data. Parentareacode = _dr["Parentareacode"]. ToString ();
List. ADD (data);
}
}
Finally
{
_conn.close ();
}
return list;
}
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Fillprov ()
{
Fetch data
list<chinastates> list = new Chianstatesda (). Select ("0001");
Fill it in.
Cmbprov.datasource = list;
Cmbprov.displaymember = "AreaName";
Cmbprov.valuemember = "AreaCode";
}
private void Fillcity ()
{
String prt = (Cmbprov.selecteditem as chinastates). AreaCode;
list<chinastates> list = new Chianstatesda (). Select (PRT);
Cmbcity.datasource = list;
Cmbcity.displaymember = "AreaName";
Cmbcity.valuemember = "AreaCode";
}
private void Fillcounty ()
{
String prt = "";
if (cmbcity.selecteditem!=null)
{
PRT = (Cmbcity.selecteditem as chinastates). AreaCode;
}
list<chinastates> list = new Chianstatesda (). Select (PRT);
Cmbcounty.datasource = list;
Cmbcounty.displaymember = "AreaName";
Cmbcounty.valuemember = "AreaCode";
}
private void Form1_Load (object sender, EventArgs e)
{
Fillprov ();
Fillcity ();
Fillcounty ();
}
private void Cmbprov_selectedindexchanged (object sender, EventArgs e)
{
Fillcity ();
Fillcounty ();
}
private void Cmbcity_selectedindexchanged (object sender, EventArgs e)
{
Fillcounty ();
}
}
Windows Form-----Content (5)