Using TreeView to Display Records from MDB File in VB.NET 2010

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.   
  33.     Private Sub SetRootNode()  
  34.         ' set root node of TreeView:  
  35.         tvData.Nodes.Add("Phone Index")  
  36.         tvData.Nodes(0).Tag = "RootDB"  
  37.         tvData.Nodes(0).ImageIndex = 0  
  38.         tvData.Nodes(0).SelectedImageIndex = 0  
  39.         ' Set Contact Name  
  40.         SetContactName()  
  41.         'Set Customer Phone  
  42.         SetCustPhone()  
  43.     End Sub  
  44.   
  45. Private Sub SetContactName()  
  46. 'set ContactName field as node  
  47.         itmNumber = datSet.Tables(MyTable).Rows.Count  
  48.         For i As Integer = 0 To itmNumber - 1  
  49. tvData.Nodes(0).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(2).ToString())  
  50.             tvData.Nodes(0).Nodes(i).Tag = "Name"  
  51.             tvData.Nodes(0).Nodes(i).ImageIndex = 2  
  52.             tvData.Nodes(0).Nodes(i).SelectedImageIndex = 2  
  53.         Next  
  54.     End Sub  
  55.   
  56. Private Sub SetCustPhone()  
  57. 'set Phone field as node  
  58.         For i As Integer = 0 To itmNumber - 1  
  59. tvData.Nodes(0).Nodes(i).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(9).ToString())  
  60.             tvData.Nodes(0).Nodes(i).Nodes(0).Tag = "Phone"  
  61.             tvData.Nodes(0).Nodes(i).Nodes(0).ImageIndex = 4  
  62.             tvData.Nodes(0).Nodes(i).Nodes(0).SelectedImageIndex = 4  
  63.         Next  
  64.     End Sub  
  65.   
  66.     Private Sub DisplayRecord(ByVal EmployeeName As String)  
  67.         ' Show some data about customer:  
  68.         Try  
  69.             ' using DataView to find record:  
  70.             Dim dv As DataView = New DataView(datSet.Tables(MyTable))  
  71.             dv.Sort = "ContactName"  
  72.             Dim i As Integer = dv.Find(EmployeeName)  
  73.             Me.BindingContext(datSet, MyTable).Position = i  
  74.             txtCompany.Text = datSet.Tables(MyTable).Rows(i).ItemArray(1).ToString()  
  75.             txtAddress.Text = datSet.Tables(MyTable).Rows(i).ItemArray(4).ToString()  
  76.             txtCity.Text = datSet.Tables(MyTable).Rows(i).ItemArray(5).ToString()  
  77.             txtCountry.Text = datSet.Tables(MyTable).Rows(i).ItemArray(8).ToString()  
  78.         Catch ex As Exception  
  79.             MessageBox.Show(ex.ToString())  
  80.         End Try  
  81.     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.

Ebook Download
View all
Learn
View all