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. private void SetRootNode()
  37. {
  38. // set root node of TreeView:
  39. tvData.Nodes.Add("Phone Index");
  40. tvData.Nodes[0].Tag = "RootDB";
  41. tvData.Nodes[0].ImageIndex = 0;
  42. tvData.Nodes[0].SelectedImageIndex = 0;
  43. //Set Contact Name
  44. SetContactName();
  45. //Set Customer Phone
  46. SetCustPhone();
  47. }
  48. private void SetContactName()
  49. {
  50. // set ContactName field as node
  51. itmNumber = datSet.Tables[MyTable].Rows.Count;
  52. for (int i = 0; i < itmNumber; i++)
  53. {
  54. tvData.Nodes[0].Nodes.Add(datSet.Tables[MyTable].Rows[i].ItemArray[2].ToString());
  55. tvData.Nodes[0].Nodes[i].Tag = "Name";
  56. tvData.Nodes[0].Nodes[i].ImageIndex = 2;
  57. tvData.Nodes[0].Nodes[i].SelectedImageIndex = 2;
  58. }
  59. }
  60. private void SetCustPhone()
  61. // set Phone field as node
  62. {
  63. for (int i = 0; i < itmNumber; i++)
  64. {
  65. tvData.Nodes[0].Nodes[i].Nodes.Add(datSet.Tables[MyTable].Rows[i].ItemArray[9].ToString());
  66. tvData.Nodes[0].Nodes[i].Nodes[0].Tag = "Phone";
  67. tvData.Nodes[0].Nodes[i].Nodes[0].ImageIndex = 4;
  68. tvData.Nodes[0].Nodes[i].Nodes[0].SelectedImageIndex = 4;
  69. }
  70. }
  71. private void DisplayRecord(string EmployeeName)
  72. // Show some data about customer:
  73. {
  74. try
  75. {
  76. // using DataView to find record:
  77. DataView dv = new DataView(datSet.Tables[MyTable]);
  78. dv.Sort = "ContactName";
  79. int i = dv.Find(EmployeeName);
  80. this.BindingContext[datSet, MyTable].Position = i;
  81. txtCompany.Text = datSet.Tables[MyTable].Rows[i].ItemArray[1].ToString();
  82. txtAddress.Text = datSet.Tables[MyTable].Rows[i].ItemArray[4].ToString();
  83. txtCity.Text = datSet.Tables[MyTable].Rows[i].ItemArray[5].ToString();
  84. txtCountry.Text = datSet.Tables[MyTable].Rows[i].ItemArray[8].ToString();
  85. }
  86. catch (Exception ex)
  87. {
  88. MessageBox.Show(ex.ToString());
  89. }
  90. }
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.