By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

Deterimine columns of dataset from code - reflection?

P: n/a
Hi!

How do I explore in code, which properties (columns) are in a datatable? I
need to run some code for each column.

I guess the answer is reflection somehow, but I dont know where to start.

If someone could point me to a relevant example, I would be most grateful.
:)

Thanks in advance

- Klaus
Nov 21 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a
Hi Klaus...

Reflection isn't necessary in this case - the DataSet / DataTable /
DataColumn object model is sufficient...

Code:

For Each t As DataTable In Me.DataSet1.Tables
MessageBox.Show(t.TableName)
For Each c As DataColumn In t.Columns
MessageBox.Show(c.ColumnName)
Next
Next

HTH

Nigel Armstrong

"Klaus Jensen" wrote:
Hi!

How do I explore in code, which properties (columns) are in a datatable? I
need to run some code for each column.

I guess the answer is reflection somehow, but I dont know where to start.

If someone could point me to a relevant example, I would be most grateful.
:)

Thanks in advance

- Klaus

Nov 21 '05 #2

P: n/a
Hi Klaus...

Reflection isn't necessary in this case - the DataSet / DataTable /
DataColumn object model is sufficient...

Code:

For Each t As DataTable In Me.DataSet1.Tables
MessageBox.Show(t.TableName)
For Each c As DataColumn In t.Columns
MessageBox.Show(c.ColumnName)
Next
Next

HTH

Nigel Armstrong

"Klaus Jensen" wrote:
Hi!

How do I explore in code, which properties (columns) are in a datatable? I
need to run some code for each column.

I guess the answer is reflection somehow, but I dont know where to start.

If someone could point me to a relevant example, I would be most grateful.
:)

Thanks in advance

- Klaus

Nov 21 '05 #3

P: n/a
Klaus,

Why everybody wants the last times to do latebinding for things which are
free to get at design time, it seems for me if the word "reflection" has
something magical.

To get all columns from a datatable than this is enough

\\\
Public Module main
Public Sub main()
Dim dt As New DataTable
dt.Columns.Add("Klaus")
dt.Columns.Add("Cor")
For Each dc As DataColumn In dt.Columns
MessageBox.Show(dc.ColumnName)
Next
End Sub
End Module
///

I hope this helps?

Cor

"Klaus Jensen" <CurseThemNastySpammers!>
Hi!

How do I explore in code, which properties (columns) are in a datatable? I
need to run some code for each column.

I guess the answer is reflection somehow, but I dont know where to start.

If someone could point me to a relevant example, I would be most grateful.
:)

Thanks in advance

- Klaus

Nov 21 '05 #4

P: n/a
Klaus,

Why everybody wants the last times to do latebinding for things which are
free to get at design time, it seems for me if the word "reflection" has
something magical.

To get all columns from a datatable than this is enough

\\\
Public Module main
Public Sub main()
Dim dt As New DataTable
dt.Columns.Add("Klaus")
dt.Columns.Add("Cor")
For Each dc As DataColumn In dt.Columns
MessageBox.Show(dc.ColumnName)
Next
End Sub
End Module
///

I hope this helps?

Cor

"Klaus Jensen" <CurseThemNastySpammers!>
Hi!

How do I explore in code, which properties (columns) are in a datatable? I
need to run some code for each column.

I guess the answer is reflection somehow, but I dont know where to start.

If someone could point me to a relevant example, I would be most grateful.
:)

Thanks in advance

- Klaus

Nov 21 '05 #5

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Why everybody wants the last times to do latebinding for things which are
free to get at design time, it seems for me if the word "reflection" has
something magical.


I should have explained what I wanted to do more specific...

I need to write a macro, which generates code based on the dataset. Ie I
need to mark the dataset (MyData.MyReallyStrongTypedDataset) and then write
code, which examines the dataset columns, and then generates a template
class exposing each column as a property.

I hope this makes sense, if not, ask me to explain myself again. :)

- Klaus
Nov 21 '05 #6

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Why everybody wants the last times to do latebinding for things which are
free to get at design time, it seems for me if the word "reflection" has
something magical.


I should have explained what I wanted to do more specific...

I need to write a macro, which generates code based on the dataset. Ie I
need to mark the dataset (MyData.MyReallyStrongTypedDataset) and then write
code, which examines the dataset columns, and then generates a template
class exposing each column as a property.

I hope this makes sense, if not, ask me to explain myself again. :)

- Klaus
Nov 21 '05 #7

P: n/a
Klaus,

When you use the standard by VBNet generated strongly typed dataset, you can
forever reach the base dataclass from which it inherits.

However I cannot find for you how that is done in your
MyData.MyReallyStrongTypedDataset

In fact is Nigel showing to you when you have that base dataset, how you can
do it. I saw his sample after that I had sand mine.

Cor
Nov 21 '05 #8

P: n/a
Klaus,

When you use the standard by VBNet generated strongly typed dataset, you can
forever reach the base dataclass from which it inherits.

However I cannot find for you how that is done in your
MyData.MyReallyStrongTypedDataset

In fact is Nigel showing to you when you have that base dataset, how you can
do it. I saw his sample after that I had sand mine.

Cor
Nov 21 '05 #9

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eG**************@TK2MSFTNGP14.phx.gbl...
In fact is Nigel showing to you when you have that base dataset, how you
can do it. I saw his sample after that I had sand mine.


Maybe I am missing a point, but I still dont know how to come from the
string "Mydata.MyStrongtypedDataset" to an object of that type. When I have
the dataset as a correctly typed dataset, Nigels og your code is great for
displaying all columns... But the first part - I still dont know. :-S

- Klaus
Nov 21 '05 #10

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eG**************@TK2MSFTNGP14.phx.gbl...
In fact is Nigel showing to you when you have that base dataset, how you
can do it. I saw his sample after that I had sand mine.


Maybe I am missing a point, but I still dont know how to come from the
string "Mydata.MyStrongtypedDataset" to an object of that type. When I have
the dataset as a correctly typed dataset, Nigels og your code is great for
displaying all columns... But the first part - I still dont know. :-S

- Klaus
Nov 21 '05 #11

P: n/a
Klaus,

You give me more and more the idea that you have a only a string
"Mydata.Mystronglytypeddataset" and absolute nothing more and want to make
from that a strongly typed dataset. I assume that I am wrong?.

Cor

Cor
"Klaus Jensen" <sp******@must.die>
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eG**************@TK2MSFTNGP14.phx.gbl...
In fact is Nigel showing to you when you have that base dataset, how you
can do it. I saw his sample after that I had sand mine.


Maybe I am missing a point, but I still dont know how to come from the
string "Mydata.MyStrongtypedDataset" to an object of that type. When I
have the dataset as a correctly typed dataset, Nigels og your code is
great for displaying all columns... But the first part - I still dont
know. :-S

- Klaus

Nov 21 '05 #12

P: n/a
Klaus,

You give me more and more the idea that you have a only a string
"Mydata.Mystronglytypeddataset" and absolute nothing more and want to make
from that a strongly typed dataset. I assume that I am wrong?.

Cor

Cor
"Klaus Jensen" <sp******@must.die>
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eG**************@TK2MSFTNGP14.phx.gbl...
In fact is Nigel showing to you when you have that base dataset, how you
can do it. I saw his sample after that I had sand mine.


Maybe I am missing a point, but I still dont know how to come from the
string "Mydata.MyStrongtypedDataset" to an object of that type. When I
have the dataset as a correctly typed dataset, Nigels og your code is
great for displaying all columns... But the first part - I still dont
know. :-S

- Klaus

Nov 21 '05 #13

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OD**************@TK2MSFTNGP14.phx.gbl...
You give me more and more the idea that you have a only a string
"Mydata.Mystronglytypeddataset" and absolute nothing more and want to make
from that a strongly typed dataset. I assume that I am wrong?.


Yes, you misunderstand me. I'll try explaining it again. :)

I already have a strong typed dataset. It is located in the MyData-namespace
and is called Mystronglytypeddataset.
Lets say the dataset has a table SomeTable with two columns:
- ID (integer)
- Name (string)

In an empty class I write this...:

Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable

End Class

Next step is, I mark the line "Private _mydatarow as
MyData.Mystronglytypeddataset.SomeTable" and run a macro, which examines the
strong typed dataset and generates code, so the class now looks like this:

Public Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable 'mark this
row and run the macro

Public Property ID() As Integer
Get
Return _mydatarow.ID
End Get
Set(ByVal Value As Integer)
_mydatarow.ID = Value
End Set
End Property

Public Property Name() As String
Get
Return _mydatarow.Name
End Get
Set(ByVal Value As String)
_mydatarow.Name = Value
End Set
End Property

End Class

I hope this shows, what I have in mind. :)
Nov 21 '05 #14

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OD**************@TK2MSFTNGP14.phx.gbl...
You give me more and more the idea that you have a only a string
"Mydata.Mystronglytypeddataset" and absolute nothing more and want to make
from that a strongly typed dataset. I assume that I am wrong?.


Yes, you misunderstand me. I'll try explaining it again. :)

I already have a strong typed dataset. It is located in the MyData-namespace
and is called Mystronglytypeddataset.
Lets say the dataset has a table SomeTable with two columns:
- ID (integer)
- Name (string)

In an empty class I write this...:

Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable

End Class

Next step is, I mark the line "Private _mydatarow as
MyData.Mystronglytypeddataset.SomeTable" and run a macro, which examines the
strong typed dataset and generates code, so the class now looks like this:

Public Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable 'mark this
row and run the macro

Public Property ID() As Integer
Get
Return _mydatarow.ID
End Get
Set(ByVal Value As Integer)
_mydatarow.ID = Value
End Set
End Property

Public Property Name() As String
Get
Return _mydatarow.Name
End Get
Set(ByVal Value As String)
_mydatarow.Name = Value
End Set
End Property

End Class

I hope this shows, what I have in mind. :)
Nov 21 '05 #15

P: n/a
Klaus,

For me the answer you describe stays the same as the first
See this

\\\
Public Module main
Public Sub main()
Dim mydat As New MyDataset
For Each dc As DataColumn In _
mydat.TableKlaus.Columns
MessageBox.Show(dc.ColumnName)
Next
End Sub
End Module
Public Class MyDataset
Inherits DataSet
Sub New()
MyBase.new()
Dim dt As New DataTable("TableKlaus")
Me.Tables.Add(dt)
dt.Columns.Add("Klaus")
dt.Columns.Add("Cor")
End Sub
Public ReadOnly Property TableKlaus() As DataTable
Get
Return Me.Tables("TableKlaus")
End Get
End Property
End Class
///

I hope this helps anyway?

Cor
Nov 21 '05 #16

P: n/a
Klaus,

For me the answer you describe stays the same as the first
See this

\\\
Public Module main
Public Sub main()
Dim mydat As New MyDataset
For Each dc As DataColumn In _
mydat.TableKlaus.Columns
MessageBox.Show(dc.ColumnName)
Next
End Sub
End Module
Public Class MyDataset
Inherits DataSet
Sub New()
MyBase.new()
Dim dt As New DataTable("TableKlaus")
Me.Tables.Add(dt)
dt.Columns.Add("Klaus")
dt.Columns.Add("Cor")
End Sub
Public ReadOnly Property TableKlaus() As DataTable
Get
Return Me.Tables("TableKlaus")
End Get
End Property
End Class
///

I hope this helps anyway?

Cor
Nov 21 '05 #17

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
For me the answer you describe stays the same as the first
See this
I hope this helps anyway?


I am afraid you do not understand, what I want to accomplish... I am not
sure how I can explain it differently...

The code you show me generates a dataset. I want code that dynamicly
examines any strong typed dataset, and generates some code/text from the
columns, that are in the strong typed dataset.

I really don't know how I can explain it differently... But I appreciate
your effort to help me. :)

- Klaus
Nov 21 '05 #18

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
For me the answer you describe stays the same as the first
See this
I hope this helps anyway?


I am afraid you do not understand, what I want to accomplish... I am not
sure how I can explain it differently...

The code you show me generates a dataset. I want code that dynamicly
examines any strong typed dataset, and generates some code/text from the
columns, that are in the strong typed dataset.

I really don't know how I can explain it differently... But I appreciate
your effort to help me. :)

- Klaus
Nov 21 '05 #19

P: n/a
Klaus,

I made a strongly typed dataset and retrieved the columns names from it,
what do you mean with a strongly typed dataset?

Only the first part is the sample how to do it, I needed a strongly typed
dataset to show, therefore I made the most simple one I could make.

Cor

"Klaus Jensen"
"Cor Ligthert" <no************@planet.nl> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
For me the answer you describe stays the same as the first
See this
I hope this helps anyway?


I am afraid you do not understand, what I want to accomplish... I am not
sure how I can explain it differently...

The code you show me generates a dataset. I want code that dynamicly
examines any strong typed dataset, and generates some code/text from the
columns, that are in the strong typed dataset.

I really don't know how I can explain it differently... But I appreciate
your effort to help me. :)

- Klaus

Nov 21 '05 #20

P: n/a
Klaus,

I made a strongly typed dataset and retrieved the columns names from it,
what do you mean with a strongly typed dataset?

Only the first part is the sample how to do it, I needed a strongly typed
dataset to show, therefore I made the most simple one I could make.

Cor

"Klaus Jensen"
"Cor Ligthert" <no************@planet.nl> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
For me the answer you describe stays the same as the first
See this
I hope this helps anyway?


I am afraid you do not understand, what I want to accomplish... I am not
sure how I can explain it differently...

The code you show me generates a dataset. I want code that dynamicly
examines any strong typed dataset, and generates some code/text from the
columns, that are in the strong typed dataset.

I really don't know how I can explain it differently... But I appreciate
your effort to help me. :)

- Klaus

Nov 21 '05 #21

P: n/a
Klaus, maybe, the following link will give you some ideas:

http://www.smithvoice.com/vbfun.htm

On the left side bar select: VB.NET and then DB Copier.net
There is a program there that generates code from a database. He will send you the source code ot his program if you email him
directly. Maybe this is what you are looking for.
james


"Klaus Jensen" <CurseThemNastySpammers!> wrote in message news:uv**************@tk2msftngp13.phx.gbl...
"Cor Ligthert" <no************@planet.nl> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
For me the answer you describe stays the same as the first
See this
I hope this helps anyway?


I am afraid you do not understand, what I want to accomplish... I am not
sure how I can explain it differently...

The code you show me generates a dataset. I want code that dynamicly
examines any strong typed dataset, and generates some code/text from the
columns, that are in the strong typed dataset.

I really don't know how I can explain it differently... But I appreciate
your effort to help me. :)

- Klaus

Nov 21 '05 #22

P: n/a
Klaus, maybe, the following link will give you some ideas:

http://www.smithvoice.com/vbfun.htm

On the left side bar select: VB.NET and then DB Copier.net
There is a program there that generates code from a database. He will send you the source code ot his program if you email him
directly. Maybe this is what you are looking for.
james


"Klaus Jensen" <CurseThemNastySpammers!> wrote in message news:uv**************@tk2msftngp13.phx.gbl...
"Cor Ligthert" <no************@planet.nl> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
For me the answer you describe stays the same as the first
See this
I hope this helps anyway?


I am afraid you do not understand, what I want to accomplish... I am not
sure how I can explain it differently...

The code you show me generates a dataset. I want code that dynamicly
examines any strong typed dataset, and generates some code/text from the
columns, that are in the strong typed dataset.

I really don't know how I can explain it differently... But I appreciate
your effort to help me. :)

- Klaus

Nov 21 '05 #23

P: n/a
On 2004-11-12, Klaus Jensen <> wrote:
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OD**************@TK2MSFTNGP14.phx.gbl...
You give me more and more the idea that you have a only a string
"Mydata.Mystronglytypeddataset" and absolute nothing more and want to make
from that a strongly typed dataset. I assume that I am wrong?.
Yes, you misunderstand me. I'll try explaining it again. :)

I already have a strong typed dataset. It is located in the MyData-namespace
and is called Mystronglytypeddataset.
Lets say the dataset has a table SomeTable with two columns:
- ID (integer)
- Name (string)

In an empty class I write this...:

Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable

End Class

Next step is, I mark the line "Private _mydatarow as
MyData.Mystronglytypeddataset.SomeTable" and run a macro, which examines the
strong typed dataset and generates code, so the class now looks like this:


Reflection isn't what you want. Reflection will only work on a compiled
assembly, and that isn't what you have.

The easiest solution here is to simply find the relevant XML file.
Don't worry about the class or the class code for the typed dataset,
find the appropriate xml file in the project and it's trivial to use an
xpath to parse out the columns in any specific table.




Public Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable 'mark this
row and run the macro

Public Property ID() As Integer
Get
Return _mydatarow.ID
End Get
Set(ByVal Value As Integer)
_mydatarow.ID = Value
End Set
End Property

Public Property Name() As String
Get
Return _mydatarow.Name
End Get
Set(ByVal Value As String)
_mydatarow.Name = Value
End Set
End Property

End Class

I hope this shows, what I have in mind. :)

Nov 21 '05 #24

P: n/a
On 2004-11-12, Klaus Jensen <> wrote:
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OD**************@TK2MSFTNGP14.phx.gbl...
You give me more and more the idea that you have a only a string
"Mydata.Mystronglytypeddataset" and absolute nothing more and want to make
from that a strongly typed dataset. I assume that I am wrong?.
Yes, you misunderstand me. I'll try explaining it again. :)

I already have a strong typed dataset. It is located in the MyData-namespace
and is called Mystronglytypeddataset.
Lets say the dataset has a table SomeTable with two columns:
- ID (integer)
- Name (string)

In an empty class I write this...:

Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable

End Class

Next step is, I mark the line "Private _mydatarow as
MyData.Mystronglytypeddataset.SomeTable" and run a macro, which examines the
strong typed dataset and generates code, so the class now looks like this:


Reflection isn't what you want. Reflection will only work on a compiled
assembly, and that isn't what you have.

The easiest solution here is to simply find the relevant XML file.
Don't worry about the class or the class code for the typed dataset,
find the appropriate xml file in the project and it's trivial to use an
xpath to parse out the columns in any specific table.




Public Class MyBusinessClass

Private _mydataset as MyData.Mystronglytypeddataset
Private _mydatarow as MyData.Mystronglytypeddataset.SomeTable 'mark this
row and run the macro

Public Property ID() As Integer
Get
Return _mydatarow.ID
End Get
Set(ByVal Value As Integer)
_mydatarow.ID = Value
End Set
End Property

Public Property Name() As String
Get
Return _mydatarow.Name
End Get
Set(ByVal Value As String)
_mydatarow.Name = Value
End Set
End Property

End Class

I hope this shows, what I have in mind. :)

Nov 21 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.