0

I want to show the names of the tables I have in my SQL Server database in a listbox using vb.net.

I tried this but it's not working:

 connetionString = "Data Source=ABDELOUAHED;Initial Catalog=table_creances;integrated security=true"
 connection = New SqlConnection(connetionString)

 sql = "select * from table_creances"

 Try
        connection.Open()
        adapter = New SqlDataAdapter(sql, connection)
        ds.Clear()
        adapter.Fill(ds)
        connection.Close()
        ListBox1.DataSource = ds.Tables(0)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

table_creances is the name of my database.

Any help is very appreciated.

1
  • 1
    SELECT name FROM sys.tables Commented Mar 22, 2016 at 15:59

2 Answers 2

1

Try use system view sys.objects

try this code:

Using connection As New SqlConnection(connetionString)
    connection.Open()
    adapter = New SqlDataAdapter("SELECT name FROM sys.tables", connection)
    Using ds = new DataSet()
        adapter.Fill(ds)
        with ListBox1
            .DataSource = ds.Tables(0)
            .DisplayMember = "name"
            .ValueMember = "name"
        end with
    End Using
End Using
Sign up to request clarification or add additional context in comments.

5 Comments

Or better yet: for tables use sys.tables
i get system.data.grid.rows on my list box
did you set sql = "SELECT name FROM sys.tables" ?
thank you very muck it's working perfectly :)
but the question should be: how to set query as DataSource of LsListBox using vb
0

Try this as your sql

sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"

3 Comments

i get system.data.grid.rows on my list box
Try setting DisplayMember and ValueMemeber of the listbox before assigning the datasource ListBox1.DisplayMember = "TABLE_NAME" ListBox1.ValueMember = "TABLE_NAME ListBox1.DataSource = ds.Tables(0)
same problem system.data.grid.row

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.