Deterimine columns of dataset from code - reflection?

  • Thread starter Thread starter Klaus Jensen
  • Start date Start date
K

Klaus Jensen

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
 
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,

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
 
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
 
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
 
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
 
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
 
Cor Ligthert said:
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. :)
 
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
 
Cor Ligthert said:
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
 
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"
 
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
 
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.
 

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

Similar Threads

Multiple Table DataSet 7
selectedrow in datatable 1
Reflection 8
basic dataset/table question 1
List Box Columns 2
LINQ and Reflection 1
Dataset Object updating 2
Column names in Dataset 2

Back
Top