Click here to Skip to main content
11,647,170 members (71,049 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 "system.data.datatable" because "system.data.datatable" 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 7:44am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You could do it like this:
        Try
            Dim SQLConnection As SqlConnection = New SqlConnection()
            SQLConnection.ConnectionString = DBconnectionWork
            Dim SQLCommand As SqlCommand = SQLConnection.CreateCommand()
            SQLCommand.CommandText = "select c.name from syscolumns c (nolock), sysobjects o (nolock) " + _
                                     "where c.id = o.id and o.name = 'tablename' " + _
                                     "  and c.name <> 'timestamp' " + _
                                     "order by colid"
            SQLConnection.Open()
            Dim myReader As SqlDataReader = SQLCommand.ExecuteReader()
            Do While myReader.Read()
                ComboBox1.Items.Add(myReader.GetString(0))
            Loop
            myReader.Close()
            SQLConnection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try
  Permalink  
v2
Comments
joshrduncan2012 at 21-Jan-13 14:41pm
   
That command doesn't work for SQL Server. syscolumns is not recognized.
Wietze Bron at 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
good
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.

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

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

  Print Answers RSS
0 jyo.net 500
1 OriginalGriff 337
2 DamithSL 310
3 Sergey Alexandrovich Kryukov 265
4 CPallini 249
0 OriginalGriff 1,277
1 jyo.net 994
2 DamithSL 946
3 Sergey Alexandrovich Kryukov 853
4 CPallini 760


Advertise | Privacy | Mobile
Web02 | 2.8.150804.2 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2015
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