Click here to Skip to main content
15,900,665 members
Articles / Programming Languages / Visual Basic

Display Database using TreeView and ListView with ADO.NET

Rate me:
Please Sign up or sign in to vote.
4.29/5 (7 votes)
11 Jun 2010CPOL2 min read 120.6K   9.5K   37   36
Using ADO.NET in Visual Studio.NET to display database in TreeView and ListView
img067.JPG

Introduction

I wrote an article earlier about how to view the database file through the TreeView control and ListView control with the use of ADO objects. I wrote the code in that article using VB6. To read the code in VB6, click here.
Now I wrote the code using VB.NET and C#. Therefore, I use ADO.NET instead of ADO.
This article show how to:

  • Use some classes from System.Data.OleDb Namespace
  • Populate TreeView with Table names and Field names
  • Populate ListView with Records of selected Table

Background

I created two projects, I wrote the code of one using C# (2003) and the code of the other using VB.NET (2003).
The demonstration Project has one Form. I add the following controls to my Form (frmDataView):

  • Two Labels (lblDatabase) to display the file name and (lblTableName) to display the table name
  • Two Buttons, one (btnLoadData) to connect with database file, the other (btnExit) to end show
  • ImageList (ImageList1) to load some icons
  • TreeView (tvData) and ListView (lvData)

About the Code

I have used two arrays as ArrayList to save tables name and fields name:
tblArray to save tables name and fldArray to save fields name.

The code contains the following procedures:

  • DataConnection() .. to connect with database file
  • GetTables() .. to fill tblArray with tables name
  • GetFields() .. to fill fldArray with fields name
  • FillTreeView() .. to fill TreeView control with tables name and fields name
  • FillListView() .. to fill ListView control with records of selected table

Please read the full code in the Form (frmDataView).

Remarks

The file (prjVB.zip) contains the VB.NET project.
The file (prjC.zip) contains the C# project.

Final Words

I hope this article is useful and helps you to display Tables, Fields and Records from database file to TreeView and ListView. Please tell me if you have any ideas or if you find any problems. Thanks to CodeProject and thanks to all.

Mostafa Kaisoun
M_Kaisoun@hotmail.com

History

  • 11th June, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: using Sql database Pin
Mostafa Kaisoun18-Apr-11 14:39
Mostafa Kaisoun18-Apr-11 14:39 
GeneralRe: using Sql database Pin
vincezed22-Apr-11 9:46
vincezed22-Apr-11 9:46 
GeneralRe: using Sql database Pin
Mostafa Kaisoun22-Apr-11 12:50
Mostafa Kaisoun22-Apr-11 12:50 
GeneralRe: using Sql database Pin
vincezed23-Apr-11 1:36
vincezed23-Apr-11 1:36 
GeneralRe: using Sql database Pin
Mostafa Kaisoun23-Apr-11 17:26
Mostafa Kaisoun23-Apr-11 17:26 
GeneralBad habbit Pin
ManfredS196114-Jun-10 23:24
ManfredS196114-Jun-10 23:24 
GeneralRe: Bad habbit Pin
Mostafa Kaisoun15-Jun-10 1:29
Mostafa Kaisoun15-Jun-10 1:29 
GeneralRemark.. Pin
Mostafa Kaisoun12-Jun-10 23:39
Mostafa Kaisoun12-Jun-10 23:39 
Using [OleDbSchemaGuid] to get the name of fields return this fields are not in the same order as its table but the fields name are sorted (A-Z),
therefore you can:
1- Create new SQL statement in the procedure [FillListView] wherein replace (*) with then fields name as ListView headers.
or:
2- Use the following procedure [GetFields] instead of the procedure in my article because the following procedure uses [DataSet] to get fields name in order as fields in the table.

VB.NET:
Private Sub GetFields(ByVal cnn As OleDbConnection, ByVal tabName As String)
Dim strSql As String = "SELECT * FROM " + tabName
Try
cnn.Open()
Dim cmdSelect As OleDbCommand = New OleDbCommand(strSql, datCon)
Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter(cmdSelect)
Dim datSet As DataSet = New DataSet
datAdp.FillSchema(datSet, SchemaType.Source)
Dim columns As DataColumnCollection = datSet.Tables(0).Columns
fldArray = New ArrayList
For Each datColumn As DataColumn In columns
fldArray.Add(datColumn.ColumnName)
Next
cnn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

C#:
private void GetFields(OleDbConnection cnn, string tabName)
{
string strSql = "SELECT * FROM " + tabName;
try
{
cnn.Open();
OleDbCommand cmdSelect = new OleDbCommand(strSql, datCon);
OleDbDataAdapter datAdp = new OleDbDataAdapter(cmdSelect);
DataSet datSet = new DataSet();
datAdp.FillSchema(datSet,SchemaType.Source);
DataColumnCollection columns = datSet.Tables[0].Columns;
fldArray = new ArrayList();
foreach (DataColumn datColumn in columns)
{
fldArray.Add(datColumn.ColumnName);
}
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
GeneralThis is no .NET Code Pin
FZelle12-Jun-10 21:53
FZelle12-Jun-10 21:53 
GeneralRe: This is no .NET Code Pin
Mostafa Kaisoun13-Jun-10 0:05
Mostafa Kaisoun13-Jun-10 0:05 
GeneralRe: This is no .NET Code Pin
ManfredS196114-Jun-10 23:19
ManfredS196114-Jun-10 23:19 
GeneralBad column order Pin
Fabrice CARUSO11-Jun-10 21:33
Fabrice CARUSO11-Jun-10 21:33 
GeneralRe: Bad column order Pin
Mostafa Kaisoun12-Jun-10 2:05
Mostafa Kaisoun12-Jun-10 2:05 
GeneralRe: Bad column order Pin
Mostafa Kaisoun12-Jun-10 5:38
Mostafa Kaisoun12-Jun-10 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.