[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?
Re: How can I dispaly the names of the fields of a table
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?
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:
ColSelection.Items.Add(dc.ColumnName)
I haven't tested it but you should probably be able to bind the data too:
vb.net Code:
ColSelection.DisplayMember = "ColumnName"
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
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?
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.
Re: How can I dispaly the names of the fields of a table
Quote:
Originally Posted by
erom11
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?