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

Passing dataset by reference to another form

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"John Sheppard" <sp**@nospam.comschreef in bericht
news:fu*********@news2.newsguy.com...
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
Jun 27 '08 #2

P: n/a
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
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:10**********************************@microsof t.com...
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

"John Sheppard" <sp**@nospam.comschreef in bericht
news:fu*********@news2.newsguy.com...
>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

Jun 27 '08 #3

P: n/a
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.DataSou rce,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
Jun 27 '08 #4

P: n/a

"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:AD**********************************@microsof t.com...
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.DataSou rce,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.
>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

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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.