Results 1 to 7 of 7

Thread: [RESOLVED] How can I dispaly the names of the fields of a table

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Resolved [RESOLVED] How can I dispaly the names of the fields of a table

    Hi, I want to display in a combobox the names of the fields of a table (table is in SQL server 2005). Any suggestions?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How can I dispaly the names of the fields of a table

    Does this help?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Re: How can I dispaly the names of the fields of a table

    Thank you for answering. It works. However, I have a small problem. I want to pass these values to a combobox. But when I am trying to do this it displays only the name of the last field.

    objDataAdapter.Fill(objDataSet, "Table_1")

    Dim dc As DataColumn

    For Each dc In objDataSet.Tables("Table_1").Columns
    ColSelection.Text = dc.ColumnName
    Next

    Do you know how can I fix this?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I dispaly the names of the fields of a table

    You're just overwriting the Text each time. If you want to add multiple items to the ComboBox then that's what you must do:
    vb.net Code:
    1. ColSelection.Items.Add(dc.ColumnName)
    I haven't tested it but you should probably be able to bind the data too:
    vb.net Code:
    1. ColSelection.DisplayMember = "ColumnName"
    2. ColSelection.DataSource = objDataSet.Tables("Table_1").Columns
    which you only need to do once, not in a loop.

    That said, that's not how I'd get the names of the columns in a SQL Server table. I'd use the SqlConnection.GetSchema method.

    http://www.vbforums.com/showthread.p...ight=getschema
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I dispaly the names of the fields of a table

    Hang on a sec! I just realised that that thread is one of yours. Why are you asking the same question again if it was answered 2 1/2 months ago?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Re: How can I dispaly the names of the fields of a table

    Thank you for answering. I had some problems with the code that's why I asked again.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I dispaly the names of the fields of a table

    Quote Originally Posted by erom11 View Post
    Thank you for answering. I had some problems with the code that's why I asked again.
    Then, instead of asking the question like it's brand new so no-one who replies has any idea that you already have a solution, you should have either provided a link to that original thread or, even better, simply posted to that thread again, and explained what problems you were having so they could be rectified. If you don't do that then people waste their time telling you things that you already know and the issues don't actually get resolved.

    So... exactly what problem(s) are you having with the original code?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width