Passing dataset by reference to another form

J

John Sheppard

Hello there I was wondering if anyone could help me,

I am trying to pass a typed dataset to a dialoged child form by reference.
I have binding sources sitting on the child form. So to refresh them I just
set their datasource. I am guessing this is probably what is causing the
problem. Is there a better way to do this?

Anyway this all works happily and things show up when the record already
exists but I have 2 problems ;
1) When I add a new row it doesnt seem to create a record correctly (its not
there when I look at the watch, but the count increases)
2) When the record already exists and I edit it the datagrid on the main
form doesnt update with any data :(

Here is my constructor code;
Public Sub New(ByVal theDs As myTypedDatast, ByVal createNew As Boolean,
Optional ByVal theMaterialId As Integer = -1, Optional ByVal theServiceId As
Integer = -1)
Me.new()
'Refresh the bindings
Me.BaseDs = theDs
Me.bsCostCodes.DataSource = Me.BaseDs
Me.bsVendor.DataSource = Me.BaseDs
Me.bsTransactions.DataSource = Me.BaseDs

If createNew Then
'MessageBox.Show(tblTransactions.Count)
bsTransactions.AddNew()
'MessageBox.Show(tblTransactions.Count))
CurrentTransaction.CostCode_Type = "M"
CurrentTransaction.ServiceID = theServiceId
CurrentTransaction.CreatedBy = myBlSingleton.currentUser.userID
CurrentTransaction.CreatedOn = Now()
CurrentTransaction.LastModifiedBy = myBlSingleton.currentUser.userID
CurrentTransaction.LastModifiedOn = Now()
CurrentTransaction.Transaction_Date = Now()
Else
tblTransactions.Find("seqNum", theMaterialId)
CurrentTransaction.LastModifiedBy = myBlSingleton.currentUser.userID
CurrentTransaction.LastModifiedOn = Now()
End If

End Sub

Private Function CurrentTransaction() As BMS.BL.dsBMS.tblTransactionsRow
If tblTransactions.Current IsNot Nothing AndAlso TypeOf
tblTransactions.Current.row Is BMS.BL.dsBMS.tblTransactionsRow Then
Return CType(tblTransactions.Current.row, BMS.BL.dsBMS.tblTransactionsRow)
Else
Return Nothing
End If
End Function

Thank you kindly
John Sheppard
 
C

Cor Ligthert[MVP]

John,

Do you expect to get any message (whatever you write later that a message
was very helpfull), to what you wrote.

I have spent some time on it, but I don't see any passing by reference, I
see all kind of objects it seems falling from air as me.bsCostCode, from
which I can think that it is a BindingSource however maybe something
complete different.

Try to make a little example from what you are doing not just passing in
your code, what is far from complete (and nobody is interested to analyse
your complete code than beginners who want to learn from it)

Not very helpful, but I hope it brings you in the right direction to make it
able for us to help you.

Cor
 
J

John Sheppard

Thanks Cor, sorry, yes after reading it again I realise it wasnt all that
clearly written. I also found an error (byVal was meant to be byRef). I
appoligies for such scrappy posting and I will try again; I very much
appreciate your time to have a look at it.

I have a parent form with the following property
Public myDataset as myTypedDataset

I have a parent form that runs the following code
dim myForm as new frmChildForm(myDataset)
myForm.showdialog

I have a child form with the following constructor
Public Sub New(ByRef theDataset As myTypedDataset)
me.myBindingSource.datasource = theDataset
End Sub

So I have a few questions
1) Is this the correct/best way to be passing datasets to other forms? How
do other people do it? I have seen people passing binding sources?
2) I am having problems with bindings, when I add a new row it doesnt seem
to create a record correctly (its not there when I look at the watch, but
the count increases). Is there something else I have to do to make it rebind
all the controls properly?
3) When the record already exists and I edit it the datagrid on the main
form doesnt update with any data. Why would this be happening? When I look
at the watch the data is in the dataset.

Thank you
John
 
C

Cor Ligthert[MVP]

John,
So I have a few questions
1) Is this the correct/best way to be passing datasets to other forms? How
do other people do it? I have seen people passing binding sources?

I seldom use by ref in fact I think never. It is only needed as you create a
new object in a child class.
In this case clearly not, be aware that passing reference types is always by
the value of its reference.
Therefore passing by value is also cheaper (but not more than I thought an
integer) than by reference.

I did never did it, but there can be in my idea not be any harm to pass the
bindingsource and use that in your child form control, but remember for
these kind as things, forever pass by value. However then you can of course
not add directly in your underlying dataset, what you do in my idea.

Be aware that you even can pass your datagridview on page one and by
casting, use the datasource of that and then again your bindingsource get
the underlying table. Something as (typed here not tested)

dim ds as DataSet =
DirectCast(DirectCast(myPassedDataGridView.DataSource,BindingSource).DataSource,DataSet)
2) I am having problems with bindings, when I add a new row it doesnt seem
to create a record correctly (its not there when I look at the watch, but
the count increases). Is there something else I have to do to make it
rebind all the controls properly?

This is hard to answer as we don't know how you add a row, there are so many
methods for that.
3) When the record already exists and I edit it the datagrid on the main
form doesnt update with any data. Why would this be happening? When I look
at the watch the data is in the dataset.
Normally it does so this is hard to say, be aware this can only after that
you completed the entering of the data including the enter, in fact an
endedit.

Cor
 
J

John Sheppard

Cor Ligthert said:
John,


I seldom use by ref in fact I think never. It is only needed as you create
a new object in a child class.
In this case clearly not, be aware that passing reference types is always
by the value of its reference.
Therefore passing by value is also cheaper (but not more than I thought an
integer) than by reference.

I did never did it, but there can be in my idea not be any harm to pass
the bindingsource and use that in your child form control, but remember
for these kind as things, forever pass by value. However then you can of
course not add directly in your underlying dataset, what you do in my
idea.

Be aware that you even can pass your datagridview on page one and by
casting, use the datasource of that and then again your bindingsource get
the underlying table. Something as (typed here not tested)

dim ds as DataSet =
DirectCast(DirectCast(myPassedDataGridView.DataSource,BindingSource).DataSource,DataSet)

I do like the idea of passing in the datasource but I changed my mind on
that because we then don't get the advantage of using the visual tools.

I might give the gridview/casting thing a shot and see if I have any luck
with it.

As for the reason I wanted to do it; It is for a popup dialogform that
edits/displays the same information from the dataset in the main form. I
didnt want to have to manually copy things from the controls to the dataset
when the user clicks ok. I thought it'd be a fairly common thing people
would try and do.
This is hard to answer as we don't know how you add a row, there are so
many methods for that.

Normally it does so this is hard to say, be aware this can only after that
you completed the entering of the data including the enter, in fact an
endedit.

Cor

Hmm, I figured problem 2 and 3 were something to do with the binding not
being set up properly, I have tried endiniting. Yes it is hard to answer
cause there are many things that would just be too verbose to paste here. I
think im just gonna have to keep trying and experimenting and eventually I
will figure out what the hell is going wrong.

As for adding rows I just use bindingsource.addnew

One thing I am doing is that I have bindingsources on the visual part of the
designer as opposed to declaring them at runtime and bindinging them with
manual code. This doesnt work on its own so what I do in my constructor is
to set the bindingsource.datasource to my referenced dataset and it
refreshes the bindings. (Im guessing this sounds confusing).....my inkling
is that this is what is causing the problems.

Public Sub New(ByRef theDataset As myTypedDataset)
me.myBindingSource.datasource = theDataset
End Sub

Thanks Cor, I very appreciate your time and your thought out answer
Regards
John
 

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