5 Replies - 285 Views - Last Post: 25 November 2018 - 02:31 AM Rate Topic: -----

#1 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 18

Pupulate combobox with table names of a SQL database

Posted 15 November 2018 - 11:03 AM

Hi
In connection to a small admin project, I am trying to get my combobox to show all the user created tables in my data base. I would rather have a solution that uses the following method:

GetSchema("TABLES") 
ComboBox1.DataSource = things
ComboBox1.DisplayMember  = stuff 



Any help would be greatly appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Pupulate combobox with table names of a SQL database

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14578
  • View blog
  • Posts: 58,441
  • Joined: 12-June 08

Re: Pupulate combobox with table names of a SQL database

Posted 15 November 2018 - 11:24 AM

what have you tried, thought about, or considered?
Was This Post Helpful? 0
  • +
  • -

#3 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 18

Re: Pupulate combobox with table names of a SQL database

Posted 15 November 2018 - 01:19 PM

Thanks for the reply. I don't have access to my code at the moment. I will post some code tomorrow:)
Was This Post Helpful? 0
  • +
  • -

#4 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 18

Re: Pupulate combobox with table names of a SQL database

Posted 21 November 2018 - 01:20 AM

Sorry for the late reply, I had some back pain, you know sittign in from of PC for too long :)

What I have done so far is that I have created some tables (each contains soem records) in my database. Users need to see which tables are there. They need it as those tables contain different information.

I made a workaround and created another table which actually contains the list of the tables in the database and pupulated the combobox with the records of that table. That was "easier" form as I am a total newbee.

However, I woudl still love to know how to pupulate the list of th tables in a combobox for example.
Was This Post Helpful? 0
  • +
  • -

#5 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 18

Re: Pupulate combobox with table names of a SQL database

Posted 24 November 2018 - 11:23 AM

OK, I got this to work. Here is the code if someone else is interested:

Try
            SQLcon.Open()'this one you haev to define yourself

            Dim MyQuery As String = "SELECT name FROM sys.tables"
            Dim MyDataAdapter As New SqlDataAdapter(MyQuery, SQLcon)
            Dim MySQLdataSet = New DataSet()

            MyDataAdapter.Fill(MySQLdataSet)

            ComBX_SQLTableNames.DataSource = MySQLdataSet.Tables(0)
            ComBX_SQLTableNames.DisplayMember = "name"
            ComBX_SQLTableNames.ValueMember = "name"
            ComBX_SQLTableNames.Text = "Select table..."

            SQLcon.Close()

        Catch ex As SqlException
            SQLcon.Close()
            MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
        Catch ex As Exception
            SQLcon.Close()
            MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
        If SQLcon.State = ConnectionState.Open Then
            SQLcon.Close()
        End If


Was This Post Helpful? 1
  • +
  • -

#6 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6645
  • View blog
  • Posts: 27,201
  • Joined: 12-December 12

Re: Pupulate combobox with table names of a SQL database

Posted 25 November 2018 - 02:31 AM

Thank you for posting back with your solution.

Some suggestions:

The Fill method will open and close the connection (if it is not already open).

Quote

The code shown in this example does not explicitly open and close the Connection. The Fill method implicitly opens the Connection that the DataAdapter is using if it finds that the connection is not already open. If Fill opened the connection, it also closes the connection when Fill is finished. This can simplify your code when you deal with a single operation such as a Fill or an Update. However, if you are performing multiple operations that require an open connection, you can improve the performance of your application by explicitly calling the Open method of the Connection, performing the operations against the data source, and then calling the Close method of the Connection. You should try to keep connections to the data source open as briefly as possible to free resources for use by other client applications.


For other readers, from sys.tables or via information_schema.tables there are ways to filter the output to just those tables of interest.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1