alexander

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.

Code

There is five procedures:

  1. Private Sub LoadCustomerData()
  2. ' for open the database file
  3. MyDataFile = Application.StartupPath & "\DataFile\Northwind.mdb"
  4. Dim MyCon As String = "provider=microsoft.jet.oledb.4.0;Password="""";data source=" & MyDataFile
  5. datCon = New OleDbConnection()
  6. datCon.ConnectionString = MyCon
  7. Dim cmdSelect As OleDbCommand = New OleDbCommand()
  8. Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter()
  9. datSet = New DataSet()
  10. Try
  11. MyTable = "Customers"
  12. ' Select all fields
  13. Dim strSql As String = "SELECT * FROM " + MyTable + " ORDER BY ContactName"
  14. cmdSelect.CommandText = strSql
  15. cmdSelect.CommandType = CommandType.Text
  16. datCon.Open() ' open connection
  17. cmdSelect.Connection = datCon
  18. datAdp.SelectCommand = cmdSelect
  19. datAdp.Fill(datSet, MyTable) ' fill dataset
  20. datCon.Close()
  21. ' if no records then exit:
  22. Dim RecCount As Integer = Me.BindingContext(datSet, MyTable).Count
  23. If RecCount = 0 Then
  24. MessageBox.Show("No records in Customer table file!")
  25. Exit Sub
  26. End If
  27. Catch ex As Exception
  28. MessageBox.Show(ex.ToString())
  29. End Try
  30. SetRootNode()
  31. End Sub
  32. Private Sub SetRootNode()
  33. ' set root node of TreeView:
  34. tvData.Nodes.Add("Phone Index")
  35. tvData.Nodes(0).Tag = "RootDB"
  36. tvData.Nodes(0).ImageIndex = 0
  37. tvData.Nodes(0).SelectedImageIndex = 0
  38. ' Set Contact Name
  39. SetContactName()
  40. 'Set Customer Phone
  41. SetCustPhone()
  42. End Sub
  43. Private Sub SetContactName()
  44. 'set ContactName field as node
  45. itmNumber = datSet.Tables(MyTable).Rows.Count
  46. For i As Integer = 0 To itmNumber - 1
  47. tvData.Nodes(0).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(2).ToString())
  48. tvData.Nodes(0).Nodes(i).Tag = "Name"
  49. tvData.Nodes(0).Nodes(i).ImageIndex = 2
  50. tvData.Nodes(0).Nodes(i).SelectedImageIndex = 2
  51. Next
  52. End Sub
  53. Private Sub SetCustPhone()
  54. 'set Phone field as node
  55. For i As Integer = 0 To itmNumber - 1
  56. tvData.Nodes(0).Nodes(i).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(9).ToString())
  57. tvData.Nodes(0).Nodes(i).Nodes(0).Tag = "Phone"
  58. tvData.Nodes(0).Nodes(i).Nodes(0).ImageIndex = 4
  59. tvData.Nodes(0).Nodes(i).Nodes(0).SelectedImageIndex = 4
  60. Next
  61. End Sub
  62. Private Sub DisplayRecord(ByVal EmployeeName As String)
  63. ' Show some data about customer:
  64. Try
  65. ' using DataView to find record:
  66. Dim dv As DataView = New DataView(datSet.Tables(MyTable))
  67. dv.Sort = "ContactName"
  68. Dim i As Integer = dv.Find(EmployeeName)
  69. Me.BindingContext(datSet, MyTable).Position = i
  70. txtCompany.Text = datSet.Tables(MyTable).Rows(i).ItemArray(1).ToString()
  71. txtAddress.Text = datSet.Tables(MyTable).Rows(i).ItemArray(4).ToString()
  72. txtCity.Text = datSet.Tables(MyTable).Rows(i).ItemArray(5).ToString()
  73. txtCountry.Text = datSet.Tables(MyTable).Rows(i).ItemArray(8).ToString()
  74. Catch ex As Exception
  75. MessageBox.Show(ex.ToString())
  76. End Try
  77. End Sub
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.