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:
- Private Sub LoadCustomerData()
-
- MyDataFile = Application.StartupPath & "\DataFile\Northwind.mdb"
- Dim MyCon As String = "provider=microsoft.jet.oledb.4.0;Password="""";data source=" & MyDataFile
- datCon = New OleDbConnection()
- datCon.ConnectionString = MyCon
- Dim cmdSelect As OleDbCommand = New OleDbCommand()
- Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter()
- datSet = New DataSet()
- Try
- MyTable = "Customers"
-
- Dim strSql As String = "SELECT * FROM " + MyTable + " ORDER BY ContactName"
- cmdSelect.CommandText = strSql
- cmdSelect.CommandType = CommandType.Text
- datCon.Open()
- cmdSelect.Connection = datCon
- datAdp.SelectCommand = cmdSelect
- datAdp.Fill(datSet, MyTable)
- datCon.Close()
-
- Dim RecCount As Integer = Me.BindingContext(datSet, MyTable).Count
- If RecCount = 0 Then
- MessageBox.Show("No records in Customer table file!")
- Exit Sub
- End If
- Catch ex As Exception
- MessageBox.Show(ex.ToString())
- End Try
- SetRootNode()
- End Sub
-
- Private Sub SetRootNode()
-
- tvData.Nodes.Add("Phone Index")
- tvData.Nodes(0).Tag = "RootDB"
- tvData.Nodes(0).ImageIndex = 0
- tvData.Nodes(0).SelectedImageIndex = 0
-
- SetContactName()
-
- SetCustPhone()
- End Sub
-
- Private Sub SetContactName()
-
- itmNumber = datSet.Tables(MyTable).Rows.Count
- For i As Integer = 0 To itmNumber - 1
- tvData.Nodes(0).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(2).ToString())
- tvData.Nodes(0).Nodes(i).Tag = "Name"
- tvData.Nodes(0).Nodes(i).ImageIndex = 2
- tvData.Nodes(0).Nodes(i).SelectedImageIndex = 2
- Next
- End Sub
-
- Private Sub SetCustPhone()
-
- For i As Integer = 0 To itmNumber - 1
- tvData.Nodes(0).Nodes(i).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(9).ToString())
- tvData.Nodes(0).Nodes(i).Nodes(0).Tag = "Phone"
- tvData.Nodes(0).Nodes(i).Nodes(0).ImageIndex = 4
- tvData.Nodes(0).Nodes(i).Nodes(0).SelectedImageIndex = 4
- Next
- End Sub
-
- Private Sub DisplayRecord(ByVal EmployeeName As String)
-
- Try
-
- Dim dv As DataView = New DataView(datSet.Tables(MyTable))
- dv.Sort = "ContactName"
- Dim i As Integer = dv.Find(EmployeeName)
- Me.BindingContext(datSet, MyTable).Position = i
- txtCompany.Text = datSet.Tables(MyTable).Rows(i).ItemArray(1).ToString()
- txtAddress.Text = datSet.Tables(MyTable).Rows(i).ItemArray(4).ToString()
- txtCity.Text = datSet.Tables(MyTable).Rows(i).ItemArray(5).ToString()
- txtCountry.Text = datSet.Tables(MyTable).Rows(i).ItemArray(8).ToString()
- Catch ex As Exception
- MessageBox.Show(ex.ToString())
- End Try
- 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.