Data Binding and Field Names

  • Thread starter Thread starter Fred Nelson
  • Start date Start date
F

Fred Nelson

I have an app I'm working on where I would like to be able to access the
field names that are returned by a stored procedure. My stored
procedure will return about 140 fields

(select * from casemast where id=inid)

lots of data in a medical records system - I'm hoping I don't need to
create a class + parameters + etc.

I know that the field names can be displayed in a DataGrid - I don't
know how to get them otherwise - I'll use any data structure that will
let me get the names!

(I've seen lots of examples of how to read them from a datareader after
you define the fields yourself.)

Is there a data structre that supports this?

Thanks very much,

Fred
 
You can iterate through the DataColumns of a DataTable or you can use the
GetName method of the DataReader. See the code below.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


Dim cn As New SqlConnection("Server=(local);Database=pubs;Integrated
Security=SSPI;")
Dim da As New SqlDataAdapter("SELECT * FROM Authors", cn)
Dim dt As New DataTable
da.Fill(dt)
For Each dc As DataColumn In dt.Columns
ListBox1.Items.Add(dc.ColumnName)
Next

Dim dr As SqlDataReader
Dim i As Integer
cn.Open()
dr = da.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection)
For i = 0 To dr.FieldCount - 1
ListBox2.Items.Add(dr.GetName(i))
Next
dr.Close()
 
Rob said:
You can iterate through the DataColumns of a DataTable or you can use the
GetName method of the DataReader. See the code below.
Rob:

Thanks very much for showing me this - this is EXACLTY what I was
looking for!

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top