1

When I run this code it is also retrieving some other fields which are not present in the table. How can I overcome this?

Dim conn As New OleDb.OleDbConnection
'Create a connection string for an Access database
Dim strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\check\a.mdb"
'Attach the connection string to the connection object
conn.ConnectionString = strConnectionString
'Open the connection
conn.Open()
Dim Restrictions() As String = {Nothing, Nothing, selected, Nothing}
Dim CollectionName As String = "Columns"
Dim dt As DataTable = conn.GetSchema(CollectionName, Restrictions)
For Each TableRow As DataRow In dt.Rows
    ComboBox1.Items.Add(TableRow.Item("COLUMN_NAME"))

The additional columns which is also retrieved are:

1.ID
2.Date create
3.Date update
4.Id
5.Lv
6.Name
7.parent Id
8.Type
9.GUID
10.Id

... and 6 more. The original schema consists of only 5 fields.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Where did you define 'selected' and what is its value? – Steve Dec 28 '15 at 09:17
  • Slected holds the table name which is selected from another another combobox – Anish choudhary Dec 28 '15 at 09:42
  • Could you explain what are these other _temporary_ fields that you see in the table returned by GetSchema? – Steve Dec 28 '15 at 13:14
  • The additional schema which is also retrived are. 1.ID 2.Date create 3.Date update 4.Id 5.Lv 6.Name 7.parent Id 8.Type 9.GUID 10.Id And are 6 more ,orginal schema is of only 5 fields – Anish choudhary Dec 28 '15 at 14:21
  • That's weird, it seems like you have a mix from more than one table. Try to change your ComboBox1.Items.Add to add a string made from the value in the column "COLUMN_NAME" and the column "TABLE_NAME" (Sorry to ask but I should be sure: Did you clear the combobox items collection before calling this right?) – Steve Dec 28 '15 at 14:41
  • Open the database in Access and then open the table in Design View. Do you see those "extra" field names there as well? – Gord Thompson Dec 29 '15 at 13:04
  • No there are no other fields – Anish choudhary Dec 30 '15 at 06:44
  • Can you provide a link to a sample "a.mdb" file that reproduces the issue? So far I have been unable to recreate it. – Gord Thompson Dec 30 '15 at 14:22
  • Ok... iam snding mdb file link.. but have changed its fileds https://www.dropbox.com/s/d28bes4ql53xt76/a.mdb?dl=0 – Anish choudhary Dec 30 '15 at 16:08

2 Answers2

1

Your problem is simply that the variable selected has the value Nothing when you insert it into the Restrictions() array. When I run the following code I only get the column names for the table named [new]:

Option Strict On

Imports System.Data.OleDb

Module Module1

    Sub Main()
        Dim connStr As String =
                "Provider=Microsoft.ACE.OLEDB.12.0;" &
                "Data Source=C:\Users\Public\test\so34490626\a.mdb"
        Using conn As New OleDbConnection(connStr)
            conn.Open()
            Dim selected As String = "new"
            Dim Restrictions() As String = {Nothing, Nothing, selected, Nothing}
            Dim CollectionName As String = "Columns"
            Dim dt As DataTable = conn.GetSchema(CollectionName, Restrictions)
            For Each TableRow As DataRow In dt.Rows
                Console.WriteLine(TableRow.Item("COLUMN_NAME"))
            Next
        End Using
    End Sub

End Module

The result is:

GENDER
MEMBER OF RISHI PRASAD
NAME OF SADHAK
PROFESSION

However, when the value of selected is Nothing ...

Dim selected As String = Nothing

... I get the "extra" columns you describe

DateCreate
DateUpdate
Id
Lv
Name
ParentId
Type
Attributes
DataType
FieldName
IndexType
SkipColumn
SpecID
Start
Width
DateDelim
DateFourDigitYear
DateLeadingZeros
DateOrder
DecimalPoint
FieldSeparator
FileType
SpecID
SpecName
SpecType
StartRow
TextDelim
TimeDelim
GENDER
MEMBER OF RISHI PRASAD
NAME OF SADHAK
PROFESSION

The reason is revealed if I change the Console.WriteLine to include the table names:

Console.WriteLine("[{0}].[{1}]", TableRow.Item("TABLE_NAME"), TableRow.Item("COLUMN_NAME"))

Then we see:

[MSysAccessStorage].[DateCreate]
[MSysAccessStorage].[DateUpdate]
[MSysAccessStorage].[Id]
[MSysAccessStorage].[Lv]
[MSysAccessStorage].[Name]
[MSysAccessStorage].[ParentId]
[MSysAccessStorage].[Type]
[MSysIMEXColumns].[Attributes]
[MSysIMEXColumns].[DataType]
[MSysIMEXColumns].[FieldName]
[MSysIMEXColumns].[IndexType]
[MSysIMEXColumns].[SkipColumn]
[MSysIMEXColumns].[SpecID]
[MSysIMEXColumns].[Start]
[MSysIMEXColumns].[Width]
[MSysIMEXSpecs].[DateDelim]
[MSysIMEXSpecs].[DateFourDigitYear]
[MSysIMEXSpecs].[DateLeadingZeros]
[MSysIMEXSpecs].[DateOrder]
[MSysIMEXSpecs].[DecimalPoint]
[MSysIMEXSpecs].[FieldSeparator]
[MSysIMEXSpecs].[FileType]
[MSysIMEXSpecs].[SpecID]
[MSysIMEXSpecs].[SpecName]
[MSysIMEXSpecs].[SpecType]
[MSysIMEXSpecs].[StartRow]
[MSysIMEXSpecs].[TextDelim]
[MSysIMEXSpecs].[TimeDelim]
[new].[GENDER]
[new].[MEMBER OF RISHI PRASAD]
[new].[NAME OF SADHAK]
[new].[PROFESSION]

The "MSys*" tables are system tables that are hidden by default in the Access user interface.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
0

Before looping on your table rows, you need to identify the valid/permanent columns of your dataTable.

To do so, you should first browse the columns collection of your datatable object. By checking the properties of each one of these columns, you will be able to identify the temporary ones. I guess it might be hidden somewhere in the 'extended properties' of the DataColumn object.

In order to identify the right property, you will go through something like this (written on the fly ...):

For each tableColumn as DataColumn in dt.Columns
    Console.WriteLine(tableColumn.[propertyName].ToString())
    ...
Next

I do not know exactly which one of the properties will let you know if the column is part of the original table fields. You will have to guess and test in order to find it. Once it's identified, you then know how to select the rows to be added to your combobox.

Philippe Grondier
  • 10,900
  • 3
  • 33
  • 72
  • Iam new to vb.net ,could you provide me code to show me how all its done.I have only four coulmns Area,Capital,Code,Continent, Country Name and Population – Anish choudhary Dec 28 '15 at 09:46