0

How to fill combobox in vb.net with column name from Microsoft Sql Server? I have table name called Table_Categories. I wish to fill the combobox with the column names of that table . I am also planning on adding column names in the table using vb.net as its front end . how can I do it ?

    Public Sub Categories()
    Dim Connect As New SqlConnection
    Dim Adapter As New SqlDataAdapter
    Dim DataTable As New System.Data.DataTable
    Dim Query As String
    ConnectionString = "Data Source=LUSPOC-PC;Initial Catalog=Sales_Invnetory;Integrated Security=True"
    Connect = New SqlConnection(ConnectionString)
    Connect.Open()
    Connect.ChangeDatabase("Sales_Inventory")
    Query = "select Column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='Table_Categories'"
    Adapter.SelectCommand = New SqlCommand(Query, Connect)

    Adapter.Fill(DataTable)
    Admin.items_category_combobx.DataSource = DataTable
    Admin.items_category_combobx.DisplayMember = "Column_name"
    Admin.items_category_combobx.ValueMember = "Column_name"

End Sub
5
  • Show us some effort. What have you tried so far? Commented Sep 1, 2016 at 6:43
  • I tried doing this Commented Sep 1, 2016 at 6:46
  • Please avoid using the comments to write code. Edit your question instead. Commented Sep 1, 2016 at 6:47
  • Don't put it as a comment. Put that in your post to help improve your question so people can help you better. Commented Sep 1, 2016 at 6:47
  • ok thanks. Im new at this ,sorry Commented Sep 1, 2016 at 6:52

2 Answers 2

0
Connect.ChangeDatabase("database-name")    
Query = "select Column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='your-Table-name'"
    Admin.items_category_combobx.DataSource = DataTable
    Admin.items_category_combobx.DisplayMember = "Column_name"
    Admin.items_category_combobx.ValueMember = "Column_name"

Change your query as above and you will get column names in your dataset. For adding column, take column name and type from user. build and execute an alter table query for adding column from it.

Sign up to request clarification or add additional context in comments.

21 Comments

what exactly is the problem?
I edited my question with the Query use sent . for you to see how I use it . is that correct ?
set displaymember and valuemember to "column_name". You have set it incorrectly
check my modified answer
you mean the rest are correct already ? I already change it to Column_name ,still nothing happens . by the way Im a module , do i need to put it on the combobox ?
|
0
          Using sqlconn As New SqlConnection("your connection string")
                sqlconn.Open()
                Using SqlCommand As New SqlCommand("select Column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='Table_Categories'", sqlconn)
                    SqlCommand.CommandTimeout = 0                     
                    Using sqlAdp As New SqlDataAdapter(SqlCommand)
                        sqlAdp.Fill(dt)
                    End Using
                End Using
        End Using
        ComboBox1.DataSource = dt
        ComboBox1.DataTextField = "Column_name" 
        ComboBox1.DataValueField = "Column_name"
        ComboBox1.DataBind()

2 Comments

do i have to declare DataTextField,DataValueField,DataBind() ? its not working
how ? Im new at this .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.