Click here to Skip to main content
13,257,667 members (73,985 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone!

I am trying to retrieve a list of columns in a particular table in the database. Here is what I have so far:

var dtCols = conn.GetSchema("Columns", new[] { "waveform_db", null, "Electrical" });

Is this a proper way? If so, how can I access this list to add to a combobox? Here is what I have so far.

if (dtCols != null)
                        foreach (var dr in dtCols)
                            // code to add to combobox

The compiler is balking at the beginning of the foreach statement.
"foreach statement cannot operate on variables of type "" because "" does not contain a public definition for "getenumerator".

I'm not real sure what this means. Does anyone have any hints as to how to resolve this issue?

Thanks everyone!
Posted 21-Jan-13 8:44am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You could do it like this:
    Dim SQLConnection As SqlConnection = New SqlConnection()
    SQLConnection.ConnectionString = DBconnectionWork
    Dim SQLCommand As SqlCommand = SQLConnection.CreateCommand()
    SQLCommand.CommandText = "select from syscolumns c (nolock), sysobjects o (nolock) " + _
                             "where = and = 'tablename' " + _
                             "  and <> 'timestamp' " + _
                             "order by colid"
    Dim myReader As SqlDataReader = SQLCommand.ExecuteReader()
    Do While myReader.Read()
Catch ex As Exception
    MsgBox(ex.ToString, MsgBoxStyle.Critical)
End Try
joshrduncan2012 21-Jan-13 14:41pm
That command doesn't work for SQL Server. syscolumns is not recognized.
Wietze Bron 21-Jan-13 14:52pm
Ofcourse it works. The SQL query is correct.
For 'tablename' you have to substitute your own table.
For DBconnectionWork you have to substitute your own connection string.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

foreach (DataRow dr in dtCols.Rows)

Your specific error is because you need to iterate the Rows of the DataTable not the DataTable itself.

Wietze Bron is correct, his method is exactly what you need.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100