Using TreeView to Display Records from MDB File in C# 2010

alexander feuer

This is a trial to read records from MDB database file using TreeView control. My project is a phone index to display some fields from Customers table in Northwind database file:

CompanyName, ContactName, Address, City, Country and Phone.

My project has one form (frmPhone), this form has following controls:

  • TreeView: tvData
  • Four TextBoxes:

txtCompany for Company Name, txtAddress for Address, txtCity for City and , txtCountry for Country.

Two Buttons: for load data and exit application.

The Code

There is five procedures:

  1.         private void LoadCustomerData()   // for open the database file  
  2.         {  
  3.             MyDataFile = Application.StartupPath + @"\DataFile\Northwind.mdb";  
  4.             string MyCon = @"provider=microsoft.jet.oledb.4.0;Password="""";data source=" + MyDataFile;  
  5.             datCon = new OleDbConnection();  
  6.             datCon.ConnectionString = MyCon;  
  7.             OleDbCommand cmdSelect = new OleDbCommand();  
  8.             OleDbDataAdapter datAdp = new OleDbDataAdapter();  
  9.             datSet = new DataSet();  
  10.             try  
  11.             {  
  12.                 MyTable = "Customers";  
  13.                 // Select all fields  
  14.                 string strSql = "SELECT * FROM " + MyTable + " ORDER BY ContactName";  
  15.                 cmdSelect.CommandText = strSql;  
  16.                 cmdSelect.CommandType = CommandType.Text;  
  17.                 datCon.Open();  // open connection  
  18.                 cmdSelect.Connection = datCon;  
  19.                 datAdp.SelectCommand = cmdSelect;  
  20.                 datAdp.Fill(datSet, MyTable);  // fill dataset  
  21.                 datCon.Close();  
  22.                 // if no records then exit:  
  23.                 int RecCount = this.BindingContext[datSet, MyTable].Count;  
  24.                 if (RecCount == 0)  
  25.                 {  
  26.                     MessageBox.Show("No records in Customer table file!");  
  27.                     return;  
  28.                 }  
  29.             }  
  30.             catch (Exception ex)  
  31.             {  
  32.                 MessageBox.Show(ex.ToString());  
  33.             }  
  34.             SetRootNode();  
  35.         }  
  36.   
  37. private void SetRootNode()  
  38.         {  
  39.             // set root node of TreeView:  
  40.             tvData.Nodes.Add("Phone Index");  
  41.             tvData.Nodes[0].Tag = "RootDB";  
  42.             tvData.Nodes[0].ImageIndex = 0;  
  43.             tvData.Nodes[0].SelectedImageIndex = 0;  
  44.             //Set Contact Name  
  45.             SetContactName();  
  46.             //Set Customer Phone  
  47.             SetCustPhone();  
  48.         }  
  49.   
  50. private void SetContactName()  
  51.         {  
  52. // set ContactName field as node  
  53.             itmNumber = datSet.Tables[MyTable].Rows.Count;  
  54.             for (int i = 0; i < itmNumber; i++)  
  55.             {  
  56. tvData.Nodes[0].Nodes.Add(datSet.Tables[MyTable].Rows[i].ItemArray[2].ToString());  
  57.                 tvData.Nodes[0].Nodes[i].Tag = "Name";  
  58.                 tvData.Nodes[0].Nodes[i].ImageIndex = 2;  
  59.                 tvData.Nodes[0].Nodes[i].SelectedImageIndex = 2;  
  60.             }  
  61.         }  
  62.   
  63. private void SetCustPhone()  
  64. // set Phone field as node  
  65.         {  
  66.             for (int i = 0; i < itmNumber; i++)  
  67.             {  
  68. tvData.Nodes[0].Nodes[i].Nodes.Add(datSet.Tables[MyTable].Rows[i].ItemArray[9].ToString());  
  69.                 tvData.Nodes[0].Nodes[i].Nodes[0].Tag = "Phone";  
  70.                 tvData.Nodes[0].Nodes[i].Nodes[0].ImageIndex = 4;  
  71.                 tvData.Nodes[0].Nodes[i].Nodes[0].SelectedImageIndex = 4;  
  72.             }  
  73.         }  
  74.   
  75. private void DisplayRecord(string EmployeeName)  
  76. // Show some data about customer:  
  77.         {  
  78.             try  
  79.             {  
  80.                 // using DataView to find record:  
  81.                 DataView dv = new DataView(datSet.Tables[MyTable]);  
  82.                 dv.Sort = "ContactName";  
  83.                 int i = dv.Find(EmployeeName);  
  84.                 this.BindingContext[datSet, MyTable].Position = i;  
  85.                 txtCompany.Text = datSet.Tables[MyTable].Rows[i].ItemArray[1].ToString();  
  86.                 txtAddress.Text = datSet.Tables[MyTable].Rows[i].ItemArray[4].ToString();  
  87.                 txtCity.Text = datSet.Tables[MyTable].Rows[i].ItemArray[5].ToString();  
  88.                 txtCountry.Text = datSet.Tables[MyTable].Rows[i].ItemArray[8].ToString();  
  89.             }  
  90.             catch (Exception ex)  
  91.             {  
  92.                 MessageBox.Show(ex.ToString());  
  93.             }  
  94.         }  
Summary

I hope that this article be useful in case of use TreeView control, so I invite you to go to source file to read the code about TreeView events. If you have any idea about this code, please tell me. Thanks for C# Corner team and thanks for all.

Ebook Download
View all
Learn
View all