473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A VERY strange problem when Updating a Dataset

A little background:

I use three Datagrids that are in a child parent relation.

I Use Negative Autoincrement on the the DataTables and that's workning nice.

My problem is when I Update these grid and write to the database and I set
the new Primary Keys and related Fields to the new asigned atuonumbers in
the Access.

The following Sub is the RowUpdated for the TopGrid's Adapter that will get
the new ID and then set that as a ParentID in Datatable for the the Next
Grid.

'---------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------------------------------------------

Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles adpProjParts.RowUpdated

If e.StatementType = StatementType.Insert Then

Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)

Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key

Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey ID

CopyDataViews()

e.Row()("PRP_ID") = NewID

e.Row().AcceptChanges()

Me.dvProjCodes_COPY.RowFilter = Nothing

Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId

Dim I As Long

Dim alRows As New ArrayList

Dim drvRowIter As DataRowView

For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1

alRows.Add(Me.dvProjCodes_COPY.Item(I))

Next

Me.dvProjCodes_COPY.RowFilter = Nothing

m_bIsSaving = True

For Each drvRowIter In alRows

drvRowIter.Row("prco_prpid") = NewID

Next

m_bIsSaving = False

End If

End Sub

'---------------------------------------------------------------------------
--------------------------

If I debug this Sub, everythign is fine and I everything seems to get the
right IDs when stepping thru it. BUT when I later check the database, One or
more of the Items in the next grid (that I just changed the Parent ID on
with this Sub) has gotten the old Negative Autonumber back.. and Of course
the relation in Access is lost.

I have been trying to figure out what rows are "changed" back to the
Negative ID and the only thing I have seen that's even remotly close, is
that it's often is the Row in the datagrid that I have my cursor on when I
trigger the Save (Update on the dataset). But that' snot completely true, it
also seems like the rows I have HAD selected and then Left also the Get
Negative ID back..

BUT.. here's the weird part.. it shouldn't matter what Rows I have selected
in the grids.. since the RowUpdated happens AFTER all that.. so I'm
completely Confused right now.

To me it seems like even though the RowUpdated Sub is working alright when
stepping thru it, some of the Items in the DataTable that I set the New
ParentID on, changes back after that...

I know it's hard to explain this problem...

PLEASE HELP!!!

/Lars Netzel




Nov 20 '05 #1
14 2097
You dont need any of this code.

Lets say you add some records right ?,, they will have negative PK's on the
Grid, once you run an update to the datasource, these will be assigned new
positive ID's providing your Acccess Corresponding PK is Autoincremented in
the design.

You can prove this.

1.) Create a table with two fields in it. One being the ID ( Primary key )
and set this to Autoincrement in Access.

2.) Put a grid on a form and bind the grid to the dataset.datatable.

3.) Put the following code in the Load event of the form
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSte p = -1
Me.OleDbDataAdapter1.Fill(Me.DataSet11)

4.) Create two buttons one for Fill and One for Update put the code in for
that.
5.) Create four entries, ( see the negative keys ).
6.) Click the Update and then Load.
7.) Bingo Your four new records all have numbers 1-4 for their PK.
8.) Updating the data in those non PK keys does not affect the PK, try it.
HTH - OHM ( Terry Burns )

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
A little background:

I use three Datagrids that are in a child parent relation.

I Use Negative Autoincrement on the the DataTables and that's workning nice.
My problem is when I Update these grid and write to the database and I set
the new Primary Keys and related Fields to the new asigned atuonumbers in
the Access.

The following Sub is the RowUpdated for the TopGrid's Adapter that will get the new ID and then set that as a ParentID in Datatable for the the Next
Grid.

'--------------------------------------------------------------------------- -------------------------------------------------------------------------- -- ------------------------------------------------------------------------

Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles adpProjParts.RowUpdated
If e.StatementType = StatementType.Insert Then

Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key

Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey ID

CopyDataViews()

e.Row()("PRP_ID") = NewID

e.Row().AcceptChanges()

Me.dvProjCodes_COPY.RowFilter = Nothing

Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId

Dim I As Long

Dim alRows As New ArrayList

Dim drvRowIter As DataRowView

For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1

alRows.Add(Me.dvProjCodes_COPY.Item(I))

Next

Me.dvProjCodes_COPY.RowFilter = Nothing

m_bIsSaving = True

For Each drvRowIter In alRows

drvRowIter.Row("prco_prpid") = NewID

Next

m_bIsSaving = False

End If

End Sub

'--------------------------------------------------------------------------- --------------------------

If I debug this Sub, everythign is fine and I everything seems to get the
right IDs when stepping thru it. BUT when I later check the database, One or more of the Items in the next grid (that I just changed the Parent ID on
with this Sub) has gotten the old Negative Autonumber back.. and Of course
the relation in Access is lost.

I have been trying to figure out what rows are "changed" back to the
Negative ID and the only thing I have seen that's even remotly close, is
that it's often is the Row in the datagrid that I have my cursor on when I
trigger the Save (Update on the dataset). But that' snot completely true, it also seems like the rows I have HAD selected and then Left also the Get
Negative ID back..

BUT.. here's the weird part.. it shouldn't matter what Rows I have selected in the grids.. since the RowUpdated happens AFTER all that.. so I'm
completely Confused right now.

To me it seems like even though the RowUpdated Sub is working alright when
stepping thru it, some of the Items in the DataTable that I set the New
ParentID on, changes back after that...

I know it's hard to explain this problem...

PLEASE HELP!!!

/Lars Netzel



Nov 20 '05 #2
The PK is not the problem... it's the ParentID (foreign key) column in the
Next table that's the problem.. which does not get the "new" ID from
access...

So that's why I use the code to Catch the New ID given by Access in the
RowUpdated Event.. and then use ID to set as the Foregin Key in the next
table.. BUT here's where the troubles comes..

Some of the Rows that I "manually" set the foregin key on are fine, but the
row that is selected in the grid... for some reason get'äs the Negative
Foreign key value back before it's saved..

/Lars

"One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
news:%2****************@TK2MSFTNGP11.phx.gbl...
You dont need any of this code.

Lets say you add some records right ?,, they will have negative PK's on the Grid, once you run an update to the datasource, these will be assigned new
positive ID's providing your Acccess Corresponding PK is Autoincremented in the design.

You can prove this.

1.) Create a table with two fields in it. One being the ID ( Primary key )
and set this to Autoincrement in Access.

2.) Put a grid on a form and bind the grid to the dataset.datatable.

3.) Put the following code in the Load event of the form
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSte p = -1
Me.OleDbDataAdapter1.Fill(Me.DataSet11)

4.) Create two buttons one for Fill and One for Update put the code in for
that.
5.) Create four entries, ( see the negative keys ).
6.) Click the Update and then Load.
7.) Bingo Your four new records all have numbers 1-4 for their PK.
8.) Updating the data in those non PK keys does not affect the PK, try it.
HTH - OHM ( Terry Burns )

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
A little background:

I use three Datagrids that are in a child parent relation.

I Use Negative Autoincrement on the the DataTables and that's workning nice.

My problem is when I Update these grid and write to the database and I set the new Primary Keys and related Fields to the new asigned atuonumbers in the Access.

The following Sub is the RowUpdated for the TopGrid's Adapter that will

get
the new ID and then set that as a ParentID in Datatable for the the Next
Grid.

'---------------------------------------------------------------------------
--------------------------------------------------------------------------
--
------------------------------------------------------------------------

Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles adpProjParts.RowUpdated

If e.StatementType = StatementType.Insert Then

Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY",

e.Command.Connection)

Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key

Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey ID

CopyDataViews()

e.Row()("PRP_ID") = NewID

e.Row().AcceptChanges()

Me.dvProjCodes_COPY.RowFilter = Nothing

Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId

Dim I As Long

Dim alRows As New ArrayList

Dim drvRowIter As DataRowView

For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1

alRows.Add(Me.dvProjCodes_COPY.Item(I))

Next

Me.dvProjCodes_COPY.RowFilter = Nothing

m_bIsSaving = True

For Each drvRowIter In alRows

drvRowIter.Row("prco_prpid") = NewID

Next

m_bIsSaving = False

End If

End Sub

'---------------------------------------------------------------------------
--------------------------

If I debug this Sub, everythign is fine and I everything seems to get the right IDs when stepping thru it. BUT when I later check the database, One or
more of the Items in the next grid (that I just changed the Parent ID on
with this Sub) has gotten the old Negative Autonumber back.. and Of
course the relation in Access is lost.

I have been trying to figure out what rows are "changed" back to the
Negative ID and the only thing I have seen that's even remotly close, is
that it's often is the Row in the datagrid that I have my cursor on when I trigger the Save (Update on the dataset). But that' snot completely true, it
also seems like the rows I have HAD selected and then Left also the Get
Negative ID back..

BUT.. here's the weird part.. it shouldn't matter what Rows I have

selected
in the grids.. since the RowUpdated happens AFTER all that.. so I'm
completely Confused right now.

To me it seems like even though the RowUpdated Sub is working alright

when stepping thru it, some of the Items in the DataTable that I set the New
ParentID on, changes back after that...

I know it's hard to explain this problem...

PLEASE HELP!!!

/Lars Netzel




Nov 20 '05 #3

How have you got these relationships defined?

You can create a relation in the dataset and bind the Details grid to this,
that way it should work.

Regards OHM (Terry Burns)

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
The PK is not the problem... it's the ParentID (foreign key) column in the
Next table that's the problem.. which does not get the "new" ID from
access...

So that's why I use the code to Catch the New ID given by Access in the
RowUpdated Event.. and then use ID to set as the Foregin Key in the next
table.. BUT here's where the troubles comes..

Some of the Rows that I "manually" set the foregin key on are fine, but the row that is selected in the grid... for some reason get'äs the Negative
Foreign key value back before it's saved..

/Lars

"One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
news:%2****************@TK2MSFTNGP11.phx.gbl...
You dont need any of this code.

Lets say you add some records right ?,, they will have negative PK's on the
Grid, once you run an update to the datasource, these will be assigned new
positive ID's providing your Acccess Corresponding PK is Autoincremented

in
the design.

You can prove this.

1.) Create a table with two fields in it. One being the ID ( Primary key ) and set this to Autoincrement in Access.

2.) Put a grid on a form and bind the grid to the dataset.datatable.

3.) Put the following code in the Load event of the form
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSte p = -1
Me.OleDbDataAdapter1.Fill(Me.DataSet11)

4.) Create two buttons one for Fill and One for Update put the code in for that.
5.) Create four entries, ( see the negative keys ).
6.) Click the Update and then Load.
7.) Bingo Your four new records all have numbers 1-4 for their PK.
8.) Updating the data in those non PK keys does not affect the PK, try it.

HTH - OHM ( Terry Burns )

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
A little background:

I use three Datagrids that are in a child parent relation.

I Use Negative Autoincrement on the the DataTables and that's workning

nice.

My problem is when I Update these grid and write to the database and I set the new Primary Keys and related Fields to the new asigned atuonumbers in the Access.

The following Sub is the RowUpdated for the TopGrid's Adapter that will get
the new ID and then set that as a ParentID in Datatable for the the
Next Grid.

'---------------------------------------------------------------------------


--------------------------------------------------------------------------
--

------------------------------------------------------------------------
Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles

adpProjParts.RowUpdated

If e.StatementType = StatementType.Insert Then

Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY",

e.Command.Connection)

Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key

Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey ID

CopyDataViews()

e.Row()("PRP_ID") = NewID

e.Row().AcceptChanges()

Me.dvProjCodes_COPY.RowFilter = Nothing

Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId

Dim I As Long

Dim alRows As New ArrayList

Dim drvRowIter As DataRowView

For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1

alRows.Add(Me.dvProjCodes_COPY.Item(I))

Next

Me.dvProjCodes_COPY.RowFilter = Nothing

m_bIsSaving = True

For Each drvRowIter In alRows

drvRowIter.Row("prco_prpid") = NewID

Next

m_bIsSaving = False

End If

End Sub

'---------------------------------------------------------------------------
--------------------------

If I debug this Sub, everythign is fine and I everything seems to get the right IDs when stepping thru it. BUT when I later check the database, One
or
more of the Items in the next grid (that I just changed the Parent ID on with this Sub) has gotten the old Negative Autonumber back.. and Of

course the relation in Access is lost.

I have been trying to figure out what rows are "changed" back to the
Negative ID and the only thing I have seen that's even remotly close, is that it's often is the Row in the datagrid that I have my cursor on when I
trigger the Save (Update on the dataset). But that' snot completely true,
it
also seems like the rows I have HAD selected and then Left also the

Get Negative ID back..

BUT.. here's the weird part.. it shouldn't matter what Rows I have

selected
in the grids.. since the RowUpdated happens AFTER all that.. so I'm
completely Confused right now.

To me it seems like even though the RowUpdated Sub is working alright

when stepping thru it, some of the Items in the DataTable that I set the New ParentID on, changes back after that...

I know it's hard to explain this problem...

PLEASE HELP!!!

/Lars Netzel





Nov 20 '05 #4
Tried that, gave the same result so the relations actually worked but some
rows still got the Wrong Foregn Key when written to the databasen... which
means the Negativ Foregin key that the Dataset Generated for the Parent
Table.

So I'm thinking it must be cause of something else.. or?

What about that it happens to the row that is selected in the Grid... any
ideas why it happens to that one?

I have checked to see if there are any other events triggred (like Position
Changed) and it was but those are handled with a Global Variable and they
don't do anything.. so I'm sure that the Save I do and all the Updates are
not interfreered by any other code I have written somewhere else... I
hope...

IT's really really frustrating, don't know where to turn anymore really...
and I really really need it done by today..

I Appiciate any help I can get here!

/Lars
"One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
news:%2****************@tk2msftngp13.phx.gbl...

How have you got these relationships defined?

You can create a relation in the dataset and bind the Details grid to this, that way it should work.

Regards OHM (Terry Burns)

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
The PK is not the problem... it's the ParentID (foreign key) column in the
Next table that's the problem.. which does not get the "new" ID from
access...

So that's why I use the code to Catch the New ID given by Access in the
RowUpdated Event.. and then use ID to set as the Foregin Key in the next
table.. BUT here's where the troubles comes..

Some of the Rows that I "manually" set the foregin key on are fine, but the
row that is selected in the grid... for some reason get'äs the Negative
Foreign key value back before it's saved..

/Lars

"One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
news:%2****************@TK2MSFTNGP11.phx.gbl...
You dont need any of this code.

Lets say you add some records right ?,, they will have negative PK's on
the
Grid, once you run an update to the datasource, these will be assigned new positive ID's providing your Acccess Corresponding PK is
Autoincremented in
the design.

You can prove this.

1.) Create a table with two fields in it. One being the ID ( Primary key ) and set this to Autoincrement in Access.

2.) Put a grid on a form and bind the grid to the dataset.datatable.

3.) Put the following code in the Load event of the form
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSte p = -1
Me.OleDbDataAdapter1.Fill(Me.DataSet11)

4.) Create two buttons one for Fill and One for Update put the code in for that.
5.) Create four entries, ( see the negative keys ).
6.) Click the Update and then Load.
7.) Bingo Your four new records all have numbers 1-4 for their PK.
8.) Updating the data in those non PK keys does not affect the PK, try it.

HTH - OHM ( Terry Burns )

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
> A little background:
>
> I use three Datagrids that are in a child parent relation.
>
> I Use Negative Autoincrement on the the DataTables and that's
workning nice.
>
> My problem is when I Update these grid and write to the database and I set
> the new Primary Keys and related Fields to the new asigned
atuonumbers in
> the Access.
>
> The following Sub is the RowUpdated for the TopGrid's Adapter that will get
> the new ID and then set that as a ParentID in Datatable for the the Next > Grid.
>
>

'---------------------------------------------------------------------------


--------------------------------------------------------------------------
--

------------------------------------------------------------------------
>
> Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As > System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles
adpProjParts.RowUpdated
>
> If e.StatementType = StatementType.Insert Then
>
> Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY",
e.Command.Connection)
>
> Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key
>
> Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey ID
>
> CopyDataViews()
>
> e.Row()("PRP_ID") = NewID
>
> e.Row().AcceptChanges()
>
> Me.dvProjCodes_COPY.RowFilter = Nothing
>
> Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId
>
> Dim I As Long
>
> Dim alRows As New ArrayList
>
> Dim drvRowIter As DataRowView
>
> For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1
>
> alRows.Add(Me.dvProjCodes_COPY.Item(I))
>
> Next
>
> Me.dvProjCodes_COPY.RowFilter = Nothing
>
> m_bIsSaving = True
>
> For Each drvRowIter In alRows
>
> drvRowIter.Row("prco_prpid") = NewID
>
> Next
>
> m_bIsSaving = False
>
> End If
>
> End Sub
>
>

'---------------------------------------------------------------------------
> --------------------------
>
> If I debug this Sub, everythign is fine and I everything seems to
get the
> right IDs when stepping thru it. BUT when I later check the
database, One
or
> more of the Items in the next grid (that I just changed the Parent
ID on > with this Sub) has gotten the old Negative Autonumber back.. and Of course
> the relation in Access is lost.
>
> I have been trying to figure out what rows are "changed" back to the
> Negative ID and the only thing I have seen that's even remotly
close, is > that it's often is the Row in the datagrid that I have my cursor on when
I
> trigger the Save (Update on the dataset). But that' snot completely

true,
it
> also seems like the rows I have HAD selected and then Left also the

Get > Negative ID back..
>
> BUT.. here's the weird part.. it shouldn't matter what Rows I have
selected
> in the grids.. since the RowUpdated happens AFTER all that.. so I'm
> completely Confused right now.
>
> To me it seems like even though the RowUpdated Sub is working
alright when
> stepping thru it, some of the Items in the DataTable that I set the

New > ParentID on, changes back after that...
>
> I know it's hard to explain this problem...
>
> PLEASE HELP!!!
>
> /Lars Netzel
>
>
>
>
>
>
>
>
>
>



Nov 20 '05 #5
Hi Lars,

Did you do that
ds.update....
ds.clear
ds.fill.........................
I know it looks not very advanced, however it is a good start to investigage
the problem in my opinion.

Cor
Nov 20 '05 #6
Yes.. I did

And the problems come already in the Update Methid!

Also I just saw that When I have saved one time and then it reloads.. I get
the right ID's set on the PK fields.. BUT since the Autoincrement is set
to -1 it starts on the last "real" Id and then count backwards.. and the
gives me a conflict off course..

/Lars
"Cor Ligthert" <no**********@planet.nl> skrev i meddelandet
news:uO*************@tk2msftngp13.phx.gbl...
Hi Lars,

Did you do that
ds.update....
ds.clear
ds.fill.........................
I know it looks not very advanced, however it is a good start to investigage the problem in my opinion.

Cor

Nov 20 '05 #7
Hi Lars,

I did not use that -1 method yet, however when you do that, did you set
after the fill as well the seed again on -1?

Cor
Nov 20 '05 #8
No, you have to se the Seed and Step Before you fill your dataset...
otherwise it's gonna use starting values depending on the PK exsisting in
table...

Then you'll get the problem of Counting backwards from a positive number..

/Lars


"Cor Ligthert" <no**********@planet.nl> skrev i meddelandet
news:uj**************@TK2MSFTNGP10.phx.gbl...
Hi Lars,

I did not use that -1 method yet, however when you do that, did you set
after the fill as well the seed again on -1?

Cor

Nov 20 '05 #9
Lars,
When you define the ForeignKeyConstraint (part of the DataRelation) you need
to set the ForeignKeyConstraint.UpdateRule to Rule.Cascade.

This will cause the child rows FK field to be updated to the parent row PK
field when the DataSet.Update command returns the PK from the database.

As OHM has stated, the DataSet will largely take care of all the update on
both the Child & Parent for you. You need to be certain that you define the
DataSet correctly is the Rub!

If you are only defining the DataRelation, you can use
DataRelation.ChildKeyConstraint to retrieve the ForeignKeyConstraint object
that was created as part of the DataRelation.

If I didn't the other day, I would strongly recommend you review the
relevant chapters in Sceppa's book!

For a good tutorial on ADO.NET as well as a good desk reference once you
know ADO.NET see David Sceppa's book "Microsoft ADO.NET - Core Reference"
from MS press.

Hope this helps
Jay
"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:uE*************@TK2MSFTNGP09.phx.gbl...
Tried that, gave the same result so the relations actually worked but some
rows still got the Wrong Foregn Key when written to the databasen... which
means the Negativ Foregin key that the Dataset Generated for the Parent
Table.

So I'm thinking it must be cause of something else.. or?

What about that it happens to the row that is selected in the Grid... any
ideas why it happens to that one?

I have checked to see if there are any other events triggred (like Position Changed) and it was but those are handled with a Global Variable and they
don't do anything.. so I'm sure that the Save I do and all the Updates are
not interfreered by any other code I have written somewhere else... I
hope...

IT's really really frustrating, don't know where to turn anymore really...
and I really really need it done by today..

I Appiciate any help I can get here!

/Lars
"One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
news:%2****************@tk2msftngp13.phx.gbl...

How have you got these relationships defined?

You can create a relation in the dataset and bind the Details grid to this,
that way it should work.

Regards OHM (Terry Burns)

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
The PK is not the problem... it's the ParentID (foreign key) column in the Next table that's the problem.. which does not get the "new" ID from
access...

So that's why I use the code to Catch the New ID given by Access in the RowUpdated Event.. and then use ID to set as the Foregin Key in the next table.. BUT here's where the troubles comes..

Some of the Rows that I "manually" set the foregin key on are fine, but
the
row that is selected in the grid... for some reason get'äs the
Negative Foreign key value back before it's saved..

/Lars

"One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
news:%2****************@TK2MSFTNGP11.phx.gbl...
> You dont need any of this code.
>
> Lets say you add some records right ?,, they will have negative PK's on the
> Grid, once you run an update to the datasource, these will be assigned new
> positive ID's providing your Acccess Corresponding PK is Autoincremented in
> the design.
>
> You can prove this.
>
> 1.) Create a table with two fields in it. One being the ID ( Primary

key )
> and set this to Autoincrement in Access.
>
> 2.) Put a grid on a form and bind the grid to the dataset.datatable.
>
> 3.) Put the following code in the Load event of the form
> Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
> Me.DataSet11.Tables(0).Columns(0).AutoIncrementSte p = -1
> Me.OleDbDataAdapter1.Fill(Me.DataSet11)
>
> 4.) Create two buttons one for Fill and One for Update put the code
in
for
> that.
> 5.) Create four entries, ( see the negative keys ).
> 6.) Click the Update and then Load.
> 7.) Bingo Your four new records all have numbers 1-4 for their PK.
> 8.) Updating the data in those non PK keys does not affect the PK,
try it.
>
>
> HTH - OHM ( Terry Burns )
>
>
>
> "Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
> news:uH**************@TK2MSFTNGP09.phx.gbl...
> > A little background:
> >
> > I use three Datagrids that are in a child parent relation.
> >
> > I Use Negative Autoincrement on the the DataTables and that's workning > nice.
> >
> > My problem is when I Update these grid and write to the database
and I set
> > the new Primary Keys and related Fields to the new asigned atuonumbers in
> > the Access.
> >
> > The following Sub is the RowUpdated for the TopGrid's Adapter that will
> get
> > the new ID and then set that as a ParentID in Datatable for the
the Next
> > Grid.
> >
> >
>

'--------------------------------------------------------------------------- >


-------------------------------------------------------------------------- > --
>

------------------------------------------------------------------------
> >
> > Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e As
> > System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles
> adpProjParts.RowUpdated
> >
> > If e.StatementType = StatementType.Insert Then
> >
> > Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY",
> e.Command.Connection)
> >
> > Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key
> >
> > Dim NewID As Long = cmd.ExecuteScalar 'This is the New PrimaryKey
ID > >
> > CopyDataViews()
> >
> > e.Row()("PRP_ID") = NewID
> >
> > e.Row().AcceptChanges()
> >
> > Me.dvProjCodes_COPY.RowFilter = Nothing
> >
> > Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId
> >
> > Dim I As Long
> >
> > Dim alRows As New ArrayList
> >
> > Dim drvRowIter As DataRowView
> >
> > For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1
> >
> > alRows.Add(Me.dvProjCodes_COPY.Item(I))
> >
> > Next
> >
> > Me.dvProjCodes_COPY.RowFilter = Nothing
> >
> > m_bIsSaving = True
> >
> > For Each drvRowIter In alRows
> >
> > drvRowIter.Row("prco_prpid") = NewID
> >
> > Next
> >
> > m_bIsSaving = False
> >
> > End If
> >
> > End Sub
> >
> >
>

'---------------------------------------------------------------------------
> > --------------------------
> >
> > If I debug this Sub, everythign is fine and I everything seems to get the
> > right IDs when stepping thru it. BUT when I later check the database, One
> or
> > more of the Items in the next grid (that I just changed the Parent ID
on
> > with this Sub) has gotten the old Negative Autonumber back.. and Of course
> > the relation in Access is lost.
> >
> > I have been trying to figure out what rows are "changed" back to the > > Negative ID and the only thing I have seen that's even remotly close,
is
> > that it's often is the Row in the datagrid that I have my cursor on when
I
> > trigger the Save (Update on the dataset). But that' snot
completely true,
> it
> > also seems like the rows I have HAD selected and then Left also the Get
> > Negative ID back..
> >
> > BUT.. here's the weird part.. it shouldn't matter what Rows I have
> selected
> > in the grids.. since the RowUpdated happens AFTER all that.. so
I'm > > completely Confused right now.
> >
> > To me it seems like even though the RowUpdated Sub is working

alright when
> > stepping thru it, some of the Items in the DataTable that I set

the New
> > ParentID on, changes back after that...
> >
> > I know it's hard to explain this problem...
> >
> > PLEASE HELP!!!
> >
> > /Lars Netzel
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
>
>



Nov 20 '05 #10
Did'nt work... still hade the selected Row in the datagrid getting the
negativ value saved in the FK field.

now we bypassed by saving everything in the database with the wrok,
negative, FK values and then ww saved the real values in an Array and then
looped thru and updated the database manually.. sort of Timepressure here!

/lars


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> skrev i meddelandet
news:eK**************@TK2MSFTNGP09.phx.gbl...
Lars,
When you define the ForeignKeyConstraint (part of the DataRelation) you need to set the ForeignKeyConstraint.UpdateRule to Rule.Cascade.

This will cause the child rows FK field to be updated to the parent row PK
field when the DataSet.Update command returns the PK from the database.

As OHM has stated, the DataSet will largely take care of all the update on
both the Child & Parent for you. You need to be certain that you define the DataSet correctly is the Rub!

If you are only defining the DataRelation, you can use
DataRelation.ChildKeyConstraint to retrieve the ForeignKeyConstraint object that was created as part of the DataRelation.

If I didn't the other day, I would strongly recommend you review the
relevant chapters in Sceppa's book!

For a good tutorial on ADO.NET as well as a good desk reference once you
know ADO.NET see David Sceppa's book "Microsoft ADO.NET - Core Reference"
from MS press.

Hope this helps
Jay
"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:uE*************@TK2MSFTNGP09.phx.gbl...
Tried that, gave the same result so the relations actually worked but some
rows still got the Wrong Foregn Key when written to the databasen... which means the Negativ Foregin key that the Dataset Generated for the Parent
Table.

So I'm thinking it must be cause of something else.. or?

What about that it happens to the row that is selected in the Grid... any ideas why it happens to that one?

I have checked to see if there are any other events triggred (like Position
Changed) and it was but those are handled with a Global Variable and they don't do anything.. so I'm sure that the Save I do and all the Updates are not interfreered by any other code I have written somewhere else... I
hope...

IT's really really frustrating, don't know where to turn anymore really... and I really really need it done by today..

I Appiciate any help I can get here!

/Lars
"One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
news:%2****************@tk2msftngp13.phx.gbl...

How have you got these relationships defined?

You can create a relation in the dataset and bind the Details grid to

this,
that way it should work.

Regards OHM (Terry Burns)

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
> The PK is not the problem... it's the ParentID (foreign key) column in
the
> Next table that's the problem.. which does not get the "new" ID from
> access...
>
> So that's why I use the code to Catch the New ID given by Access in the > RowUpdated Event.. and then use ID to set as the Foregin Key in the next > table.. BUT here's where the troubles comes..
>
> Some of the Rows that I "manually" set the foregin key on are fine, but the
> row that is selected in the grid... for some reason get'äs the Negative > Foreign key value back before it's saved..
>
> /Lars
>
> "One Handed Man ( OHM#)" <news.microsoft.com> skrev i meddelandet
> news:%2****************@TK2MSFTNGP11.phx.gbl...
> > You dont need any of this code.
> >
> > Lets say you add some records right ?,, they will have negative
PK's on
> the
> > Grid, once you run an update to the datasource, these will be assigned new
> > positive ID's providing your Acccess Corresponding PK is

Autoincremented
> in
> > the design.
> >
> > You can prove this.
> >
> > 1.) Create a table with two fields in it. One being the ID (
Primary key )
> > and set this to Autoincrement in Access.
> >
> > 2.) Put a grid on a form and bind the grid to the dataset.datatable. > >
> > 3.) Put the following code in the Load event of the form
> > Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
> > Me.DataSet11.Tables(0).Columns(0).AutoIncrementSte p = -1
> > Me.OleDbDataAdapter1.Fill(Me.DataSet11)
> >
> > 4.) Create two buttons one for Fill and One for Update put the code in for
> > that.
> > 5.) Create four entries, ( see the negative keys ).
> > 6.) Click the Update and then Load.
> > 7.) Bingo Your four new records all have numbers 1-4 for their PK.
> > 8.) Updating the data in those non PK keys does not affect the PK, try it.
> >
> >
> > HTH - OHM ( Terry Burns )
> >
> >
> >
> > "Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
> > news:uH**************@TK2MSFTNGP09.phx.gbl...
> > > A little background:
> > >
> > > I use three Datagrids that are in a child parent relation.
> > >
> > > I Use Negative Autoincrement on the the DataTables and that's workning
> > nice.
> > >
> > > My problem is when I Update these grid and write to the database and
I
> set
> > > the new Primary Keys and related Fields to the new asigned

atuonumbers
> in
> > > the Access.
> > >
> > > The following Sub is the RowUpdated for the TopGrid's Adapter

that will
> > get
> > > the new ID and then set that as a ParentID in Datatable for the

the Next
> > > Grid.
> > >
> > >
> >
>

'---------------------------------------------------------------------------
> >
>


--------------------------------------------------------------------------
> > --
> >

------------------------------------------------------------------------
> > >
> > > Private Sub adpProjParts_RowUpdated(ByVal sender As Object, ByVal e
As
> > > System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles
> > adpProjParts.RowUpdated
> > >
> > > If e.StatementType = StatementType.Insert Then
> > >
> > > Dim cmd As New OleDb.OleDbCommand("SELECT @@IDENTITY",
> > e.Command.Connection)
> > >
> > > Dim OldId As Long = e.Row()("PRP_ID") 'This is primary key
> > >
> > > Dim NewID As Long = cmd.ExecuteScalar 'This is the New
PrimaryKey
ID > > >
> > > CopyDataViews()
> > >
> > > e.Row()("PRP_ID") = NewID
> > >
> > > e.Row().AcceptChanges()
> > >
> > > Me.dvProjCodes_COPY.RowFilter = Nothing
> > >
> > > Me.dvProjCodes_COPY.RowFilter = "prco_prpid=" & OldId
> > >
> > > Dim I As Long
> > >
> > > Dim alRows As New ArrayList
> > >
> > > Dim drvRowIter As DataRowView
> > >
> > > For I = Me.dvProjCodes_COPY.Count - 1 To 0 Step -1
> > >
> > > alRows.Add(Me.dvProjCodes_COPY.Item(I))
> > >
> > > Next
> > >
> > > Me.dvProjCodes_COPY.RowFilter = Nothing
> > >
> > > m_bIsSaving = True
> > >
> > > For Each drvRowIter In alRows
> > >
> > > drvRowIter.Row("prco_prpid") = NewID
> > >
> > > Next
> > >
> > > m_bIsSaving = False
> > >
> > > End If
> > >
> > > End Sub
> > >
> > >
> >
>

'--------------------------------------------------------------------------- > > > --------------------------
> > >
> > > If I debug this Sub, everythign is fine and I everything seems to get
> the
> > > right IDs when stepping thru it. BUT when I later check the

database,
> One
> > or
> > > more of the Items in the next grid (that I just changed the
Parent
ID
on
> > > with this Sub) has gotten the old Negative Autonumber back.. and

Of > course
> > > the relation in Access is lost.
> > >
> > > I have been trying to figure out what rows are "changed" back to the > > > Negative ID and the only thing I have seen that's even remotly

close,
is
> > > that it's often is the Row in the datagrid that I have my cursor on when
> I
> > > trigger the Save (Update on the dataset). But that' snot completely > true,
> > it
> > > also seems like the rows I have HAD selected and then Left also the Get
> > > Negative ID back..
> > >
> > > BUT.. here's the weird part.. it shouldn't matter what Rows I
have > > selected
> > > in the grids.. since the RowUpdated happens AFTER all that.. so

I'm > > > completely Confused right now.
> > >
> > > To me it seems like even though the RowUpdated Sub is working

alright
> when
> > > stepping thru it, some of the Items in the DataTable that I set the New
> > > ParentID on, changes back after that...
> > >
> > > I know it's hard to explain this problem...
> > >
> > > PLEASE HELP!!!
> > >
> > > /Lars Netzel
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #11
Lars,
This is NOT that difficult! It should "just work"! (It "just works" for me!
:-))

How are you creating the DataSet?
How are you creating the DataRelation?

Are you calling DataAdapter.Update on the Parent before you call
DataAdapter.Update on the children?
sort of Timepressure here! Understand. Sceppa's book fully explains the how & why to get it to work.
(It might be quicker to sit down with the book & review the relevant
chapter).

BTW: Reviewing Sceppa's book, the Rule.Cascade on the ForeignKeyConstraint
is the default.

Is this VS.NET 2002, VS.NET 2003, or VS.NET 2005 (the Whidbey preview)?

Hope this helps
Jay

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:OA*************@TK2MSFTNGP10.phx.gbl... Did'nt work... still hade the selected Row in the datagrid getting the
negativ value saved in the FK field.

now we bypassed by saving everything in the database with the wrok,
negative, FK values and then ww saved the real values in an Array and then
looped thru and updated the database manually.. sort of Timepressure here!

/lars

<<snip>>
Nov 20 '05 #12
This is the first time I have seen the shell of your cool composure eroded.

:-)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
Lars,
This is NOT that difficult! It should "just work"! (It "just works" for me! :-))

How are you creating the DataSet?
How are you creating the DataRelation?

Are you calling DataAdapter.Update on the Parent before you call
DataAdapter.Update on the children?
sort of Timepressure here!

Understand. Sceppa's book fully explains the how & why to get it to work.
(It might be quicker to sit down with the book & review the relevant
chapter).

BTW: Reviewing Sceppa's book, the Rule.Cascade on the ForeignKeyConstraint
is the default.

Is this VS.NET 2002, VS.NET 2003, or VS.NET 2005 (the Whidbey preview)?

Hope this helps
Jay

"Lars Netzel" <[stop_spam]@host.topdomain> wrote in message
news:OA*************@TK2MSFTNGP10.phx.gbl...
Did'nt work... still hade the selected Row in the datagrid getting the
negativ value saved in the FK field.

now we bypassed by saving everything in the database with the wrok,
negative, FK values and then ww saved the real values in an Array and then looped thru and updated the database manually.. sort of Timepressure here!
/lars

<<snip>>

Nov 20 '05 #13
OHM,
I didn't think I was showing erosion as much as attempting to "walking
softly with a big stick" ;-)

For some reason Lars is having trouble with this, and I don't see that this
is that difficult, especially if you read Sceppa's book, as Sceppa IMHO
provides a clear explanation of what is going on. Now if Lars was
implementing IBindingList itself, I could see Lars having problems as
IBindingList is a very interesting interface to FULLY implement...

I suspect there is something simply in Lars code that he is not telling us
as to why its not working for him...

Just a thought
Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
This is the first time I have seen the shell of your cool composure eroded.
:-)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

<<snip>>
Nov 20 '05 #14
Yeah, I thinks its one of those things, if you were there at the pc you
would probably spot it right away.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Ob**************@TK2MSFTNGP12.phx.gbl...
OHM,
I didn't think I was showing erosion as much as attempting to "walking
softly with a big stick" ;-)

For some reason Lars is having trouble with this, and I don't see that this is that difficult, especially if you read Sceppa's book, as Sceppa IMHO
provides a clear explanation of what is going on. Now if Lars was
implementing IBindingList itself, I could see Lars having problems as
IBindingList is a very interesting interface to FULLY implement...

I suspect there is something simply in Lars code that he is not telling us
as to why its not working for him...

Just a thought
Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:ev**************@tk2msftngp13.phx.gbl...
This is the first time I have seen the shell of your cool composure

eroded.

:-)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

<<snip>>

Nov 20 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
1
by: Geraldine Hobley | last post by:
hello, I have put thisup b4, but I think that people have misunderstood the problem, so I will make it clearer. I have a problem updating a dataset that is bound to a datagrid. The code that I...
10
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1)...
2
by: Niels Jensen | last post by:
I have a some code which imports information from a text file to a dataset. Depending op what is being extracted from the text file, the dataset has 5 tables which can be written to. During this...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has...
2
by: susan.f.barrett | last post by:
Hi, Despite me being able to type the following in to SQL Server and it updating 1 row: > updatestockcategory 1093, 839 In my code, it is not updating any rows. dataSet = new DataSet();
6
by: mike11d11 | last post by:
I'm trying to create an application that will have multiple users working off a table on a SQL server. Since multi users will be updating different records at any given moment, how can i get those...
0
by: OldStd | last post by:
Updating data using 2 data sets I am having some problems in updating the database using two datasets as suggested by someone. 1. Data is displayed in a data grid from a dataset generated using...
1
by: jonbartlam | last post by:
Hi There I'm not sure what exactly is going wrong here. I'm writing an application that retreives a table from a database (tbl_internalfaults) and updates it. (Actually, just the status column will...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.