Adding Tables to DataSet in Component

C

cjobes

Hi all,

I'm trying to create a dataset as a component to make it accessable from all
forms. I have used that following code so far in the component designer:

Inherits System.ComponentModel.Component
Dim _TryDs As dataSet = NewTry

Public Property dataSet() As dataSet
Get
Return Me._TryDs
End Get
Set(ByVal Value As dataSet)
Me._TryDs = Value
End Set
End Property

Now I have no idea how to add the 2 tables. I have created 2 subs
"createTableA" and "createTableB" and added the commands
createTableA()
createTableB()
after the initialize component line.

When I try to reference the component the DS is recognized but the tables
are not. I know I'm missing something here. Can anybody help?
Thanks,
Claus
 
C

Cor Ligthert

Cjobes,

Does this most simple sample that I once made for somebody give you an idea?

\\\
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
///

dim ds as new MyDataset
dim dt as datatable = ds.TableKlaus

I hope this gives some ideas?

Cor
 
C

cjobes

Cor,

Thanks a lot. I'm starting to understand this. I assume that your sample
code goes into the component code. I have two more questions:
I need to create 2 tables. Would I do this in the same public class? Also,
could you give me a short sample of what I need to do in a form to create
and instance of the 2 tables?

Actually there is one more question. If I fill the tables with data in one
form and then create an instance of the tables in another form will the data
still be there?

Thanks,
Claus
 
C

Cor Ligthert

Claus,
Thanks a lot. I'm starting to understand this. I assume that your sample
code goes into the component code.

You cannot inherit twice however the dataset that is inherited does that
already see in top of this page
http://msdn.microsoft.com/library/d...ref/html/frlrfsystemdatadatasetclasstopic.asp
I have two more questions:
I need to create 2 tables. Would I do this in the same public class? Also,
could you give me a short sample of what I need to do in a form to create
and instance of the 2 tables?

Everything you do is up to you, however why not. I showed the things above
in a new sample bellow however how far you go is again up to you. I made a
very simple sample where I made an extra datatable and load a datatable as
method in this extra and showed that table on a datagrid

\\\needs new project with a datagrid on the form
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New MyDataset
For i As Integer = 0 To 10
ds.KlausTableAddNew(New Object() {i.ToString, ChrW(i + 65)})
Next
DataGrid1.DataSource = ds.TableKlaus
End Sub
End Class
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")
dt = New DataTable("TableCor")
Me.Tables.Add(dt)
dt.Columns.Add("John")
dt.Columns.Add("Herfried")
End Sub
Public ReadOnly Property TableKlaus() As DataTable
Get
Return Me.Tables("TableKlaus")
End Get
End Property
Public ReadOnly Property TableCor() As DataTable
Get
Return Me.Tables("TableCor")
End Get
End Property
Public Sub KlausTableAddNew(ByVal obj As Object())
Dim dr As DataRow = Me.TableKlaus.NewRow
dr.ItemArray = obj
Me.TableKlaus.Rows.Add(dr)
End Sub
End Class
///

I hope this helps a little bit?

Cor
 
C

Cor Ligthert

Claus,

I see I deleted with pasting in the sample a part I had answered as well
If I fill the tables with data in one
form and then create an instance of the tables in another form will the
data
still be there?
That class is nothing, the object you create from it holds the data, and
than it is the same as whatever you create.

Assuming this is for a winform (it is not the same for a webform), than
means when you do

Friend ds as new mydataset

somewhere in your project, than it will everywhere in your project be usable
with the information in it. (As long as you do not set it too something
else of course)

I hope this helps?

Cor
 
C

cjobes

Thanks Cor,

The handling of the additional table is what I thought but you have me
totally confused with "needs new project". I think I need some tutoring to
cut the learning curve in half. I'm happy to pay you for your time. Can you
contact me at (e-mail address removed)?

Claus
 
C

cjobes

Thanks again Cor,

From your response I assume that you are not interested in making some
"tutor money". If you change your mind, you have my email.

I will try your suggestions tonight. Hopefully I understood everything
right. I'm a network guy with some experience in VB, so it will be very
interesting to say the least.

Thanks again, I really appreciate your patient help.

Claus
 
C

cjobes

Cor,

Sorry to be so stupid but I must still be doing something wrong. I created
the component and defined the tables. I then created an instance in form1
and manipulated the data. As a test I brought a DataGrid up in form1 and it
displays the data correctly.
I created a second form (SearchResult) with a DataGrid(ResultGrid) and added
the following code:

Dim ds As GlobalDS.MyDataset
Dim dt As DataTable = ds.tbSelect

Private Sub SearchResult_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ResultGrid.DataMember = "dt"
Me.ResultGrid.DataSource = ds
End Sub

On form1 I added the code:
Dim frmResults As New SearchResult()
frmResults.ShowDialog()

When I run Debug the program stops at:
Dim dt as DataTable=ds.tbSelect
with an error "Object reference not set to an instance of an object"

What am I doing wrong?

Claus
 
C

Cor Ligthert

Claus,

What I cannot see is if Global.Dataset is an object or the class itself.
When it is the class or a member of that than it has to be probably
dim ds as "new" GlobalDS.MyDataset

I hope this helps?

Cor
 
C

cjobes

Cor,

if I dim ds as New GlobalDS.MyDataset the program runs but the DataGrid on
the second form is empty, so I thought I had to leave the NEW out on the
second form.

I guess that was wrong.

Claus
 
C

cjobes

Cor,

I played around with this a bit further. When I add this code to Form1

Dim frmResults As New SearchResult()
frmResults.DataSet = ds
frmResults.ShowDialog()

and this code to Form2

Public WriteOnly Property DataSet() As DataSet
Set(ByVal Value As DataSet)
ResultGrid.DataSource = Value
End Set
End Property

the DataGrid displays the correct data. But I also need to be able to
further manipulate the data in both tables on Form2 and I don't seem to be
able to do that.

Claus
 
C

cjobes

Found a way to do it and thanks to Charlie I got the final hiccup out as
well.

Thanks for your help Cor, knowing my skills it will not be the last time I'm
asking for help in here.

Claus
 

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

Top