更新数据集时出现一个非常奇怪的问题 [英] A VERY strange problem when Updating a Dataset

查看:73
本文介绍了更新数据集时出现一个非常奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一点背景:


我使用三个处于子父关系的Datagrids。


我使用负自动增量DataTables和那个很好用。


我的问题是当我更新这些网格并写入数据库并设置

新的主键和相关字段到新的签名atuonumbers

访问。


以下Sub是TopGrid的适配器的RowUpdated将获得

新ID,然后将其设置为Datatable中的ParentID,用于Next

网格。


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

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

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


Private Sub adpProjParts_RowUpdated( ByVal sender As Object,ByVal e As

System.Data.OleDb.OleDbRow UpdatedEventArgs)处理adpProjParts.RowUpdated


如果e.StatementType = StatementType.Insert那么


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


Dim OldId As Long = e.Row()(" PRP_ID")''这是主键


Dim NewID As Long = cmd.ExecuteScalar''这是新的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 ))


下一页


Me.dvProjCodes_COPY.RowFilter = Nothing


m_bIsSaving = True


每个drvRowIter in alRows


drvRowIter.Row(" prco_prpid")= NewID


下一页


m_bIsSaving = False


结束如果


End Sub


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

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


如果我调试这个Sub,每一个都很好,我似乎一切都得到了

通过它踩踏时的正确ID。但是当我稍后检查数据库时,下一个网格中的一个或多个
更多的项目(我刚刚使用此Sub更改了

上的父ID)已经获得了旧的负自动编号返回...当然

Access中的关系丢失了。


我一直试图弄清楚哪些行被更改了回到

否定身份证,我唯一看到的就是那个甚至非常接近的东西,是

,它通常是数据网格中的行当我按
触发Save(数据集更新)时,我将光标打开。但是那个'鼻涕完全正确,它

看起来也像我已经选择的行然后左边还得到了获取

否定身份证......


但是......这是奇怪的部分..我选择的行是什么并不重要

在网格中...因为RowUpdated发生在所有这些之后..所以我是

现在完全混淆了。


对我来说,好像即使RowUpdated Sub工作正常

踩到它,DataTable中我设置New

ParentID的一些项目在之后改变了......


我知道这个问题很难解释......


请帮助!!!


/ Lars Netzel



解决方案

您不需要任何此代码。


让我们说你添加了一些正确的记录吗?一旦你运行更新,你就会在

网格上产生负面的PK'数据源,这些将被分配新的

正面ID'提供您的Acccess对应PK自动增加

设计。


你可以证明这一点。


1.)创建一个包含两个字段的表。一个是ID(主键)

并将其设置为Access中的Autoincrement。


2.)在表单上放置一个网格并将网格绑定到datat.datatable。


3.)将以下代码放在表格的加载事件中

Me.DataSet11.Tables(0).Columns( 0).AutoIncrementSee d = -1

Me.DataSet11.Tables(0).Columns(0).AutoIncrementSte p = -1

Me.OleDbDataAdapter1.Fill(Me .DataSet11)


4.)创建两个按钮,一个用于填充,一个用于更新,将代码放入

,即。

)创建四个条目,(参见否定键)。

6.)单击更新,然后单击加载。

7.)宾果你的四个新记录全部PK的数字为1-4。

8.)更新那些非PK键的数据不会影响PK,试试吧。

HTH - OHM(Terry烧伤)


" Lars Netzel"百分比抑制率stop_spam] @ host.topdomain>在消息中写道

news:uH ************** @ TK2MSFTNGP09.phx.gbl ...

一点背景:

我使用了三个处于子父关系中的Datagrids。

我在DataTables上使用负自动增量,并且工作
很好。
我的问题是当我更新这些网格并写入数据库时​​,我将新的主键和相关字段设置为访问中的新签名atuonumbers。

以下Sub是TopGrid适配器的RowUpdated,它将
获取新ID,然后将其设置为Datatable中用于Next
Grid的ParentID。


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

Private Sub adpProjParts_RowUpdated(ByVal sender As Object,ByVal e as adpProjParts.RowUpdated
如果e.StatementType = StatementType.Insert那么

Dim cmd As New OleDb.OleDbCommand(" SELECT) @@ IDENTITY",
e.Command.Connection)
Dim OldId As Long = e.Row()(" PRP_ID")''这是主键

Dim NewID As Long = cmd.ExecuteScalar''这是新的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到0步-1 -



.dvProjCodes_COPY.RowFilter = Nothing

m_bIsSaving = True



drvRowIter.Row(" prco_prpid")= NewID

下一页

m_bIsSaving = False

结束如果

结束子


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

如果我调试这个Sub,那么每一个都很好,而且当我踩到它时,我似乎都得到了正确的ID。但是当我稍后检查数据库时,下一个网格中的一个
或更多的项目(我刚用这个Sub更改了
上的父ID)已经得到了旧的负自动编号...当然
Access中的关系丢失了。

我一直试图弄清楚哪些行被改变了。回到
否定ID和我所看到的唯一一个甚至非常接近的东西是,当我<我通常是数据网格中的行时,我将光标放在br />触发Save(数据集更新)。但是那个'鼻涕完全是真的,
它看起来像我已经选择的行然后离开也得到了
否定ID ...

但是......这是奇怪的部分..在网格中选择
的行是不重要的..因为RowUpdated发生在所有这些之后......所以我现在完全混淆了。

对我来说,好像即使RowUpdated Sub在通过它踩到它时工作正常,DataTable中的一些项目,我将New
ParentID设置为,更改之后回来......

我知道很难解释这个问题...

请帮助!!!

/ Lars Netzel




PK不是问题......它是ParentID(外国人)

下一个表中的那一行是问题..哪个没有得到新的 ID来自

访问...


这就是为什么我使用代码来获取Access中给出的新ID的原因

RowUpdated Event ..然后使用ID在下一个

表中设置为Foregin Key ..但是这里的麻烦来自......

一些我手动行的行设置foregin键是正常的,但是在网格中选择了

行...由于某种原因得到''s的否定

外键值返回在它被保存之前..


/ Lars


一手人(OHM#) < news.microsoft.com> skrev i meddelandet

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...

你不要需要任何这些代码。

让我们说你添加一些记录吗?,一旦你运行数据源的更新,它们将在
网格上有负PK',这些将被分配新的
正面ID,提供你的Acccess对应PK在设计中自动增加


你可以证明这一点。

1 。)创建一个包含两个字段的表。一个是ID(主键)
并将其设置为Access中的Autoincrement。

2.)在表单上放置网格并将网格绑定到dataset.datatable。

3.)将以下代码放在表格的加载事件中。
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
Me.DataSet11。表(0).Columns(0).AutoIncrementSte p = -1
Me.OleDbDataAdapter1.Fill(Me.DataSet11)

4.)创建两个按钮,一个用于填充,一个用于更新将代码放入
中。
5.)创建四个条目,(参见否定键)。
6.)单击更新,然后加载。
7.) Bingo你的四个新记录的PK都有1-4号。
8.)更新那些非PK键中的数据不会影响PK,试试吧。

HTH - OHM (特里伯恩斯)

Lars Netzel百分比抑制率stop_spam] @ host.topdomain>在消息中写道
新闻:uH ************** @ TK2MSFTNGP09.phx.gbl ...

一点背景:

我使用三个处于子父关系的Datagrids。

我在DataTables上使用负自动增量并且工作得很好。


我的问题是,当我更新这些网格并写入数据库并且I
将新的主键和相关字段设置为Access中的新签名atuonumbers


以下Sub是TopGrid适配器的RowUpdated,它将


获取

新ID,然后将其设置为Datatable中的ParentID为Next
网格。



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

-------- -------------------- --------------------------------------------
System.Data.OleDb.OleDbRowUpdatedEventArgs)处理adpProjParts.RowUpdated


如果e.StatementType = StatementType.Insert然后

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


e.Command.Connection)


Dim OldId As Long = e.Row()(" PRP_ID")''这是主键

Dim NewID As Long = cmd.ExecuteScalar''这是新的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到0步-1 -



.dvProjCodes_COPY.RowFilter = Nothing

m_bIsSaving = True



drvRowIter.Row(" prco_prpid")= NewID

下一页

m_bIsSaving = False

结束如果

End Sub



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

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

如果我调试这个Sub,那么每一个都很好,我似乎一切都好通过它获取
正确的ID。但是当我稍后检查数据库时,
一个或

下一个网格中的更多项目(我刚用这个Sub更改了
上的父ID)已经获得了旧的负自动编号返回...和


当然Access中的关系丢失了。

我一直试图弄清楚哪些行被更改了。回到
否定ID和我所看到的唯一一个甚至非常接近的东西,就是数据网格中的Row通常是$ b时我的光标所在$ b我触发Save(数据集更新)。但是那''完全哼了一声b $ b,它

也好像我已经选择了HAD的行然后左边还得到了Get
Negative ID ..

但是......这是奇怪的部分..在网格中选择

并不是什么行


因为RowUpdated发生在所有这些之后...所以我现在完全感到困惑。

对我来说,好像即使RowUpdated Sub工作正常

<踩到它时,
,我设置New
ParentID的DataTable中的一些项目会在之后发生变化......

我知道它'很难解释这个问题...

请帮助!!!

/ Lars Netzel







你如何定义这些关系?


您可以在数据集和bin中创建关系d详细信息网格到这个,

它应该如何工作。


问候OHM(特里伯恩斯)


Lars Netzel百分比抑制率stop_spam] @ host.topdomain>在消息中写道

新闻:eX ************** @ TK2MSFTNGP12.phx.gbl ...

PK不是问题...
下一个表格中的ParentID(外键)列是问题所在......它没有得到新字样。来自
访问的ID ...

这就是为什么我使用代码来获取在RowUpdated事件中由Access提供的新ID ...然后使用ID在下一张表格中设置为Foregin Key ..但是这里是麻烦来的地方..

一些我手动行的行。设置foregin键是好的,但是
在网格中选择的行...出于某种原因,在保存之前得到'否定的外键值'。

/ Lars

一手汉语(OHM#) < news.microsoft.com> skrev i meddelandet
新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...

你不需要任何这个代码。

让我们说你添加了一些记录吗?他们将在

Grid上有负面的PK',一旦你对数据源进行了更新,这些将被分配
new

正面ID'在


post_quotes>设计。

你可以证明这一点。

1.)创建一个包含两个字段的表。一个是ID(主
键)并将其设置为Access中的自动增量。

2.)在表单上放置网格并将网格绑定到dataset.datatable。

3.)将以下代码放在表格的加载事件中。
Me.DataSet11.Tables(0).Columns(0).AutoIncrementSee d = -1
Me.DataSet11。表(0).Columns(0).AutoIncrementSte p = -1
Me.OleDbDataAdapter1.Fill(Me.DataSet11)

4.)创建两个按钮,一个用于填充,一个用于更新把代码放在
中。
5.)创建四个条目,(参见否定键)。
6.)单击Update,然后加载。
7.) Bingo你的四个新唱片的PK都有1-4号。
8.)更新那些非PK键的数据不会影响PK,试试
吧。
HTH - OHM(Terry Burns)

Lars Netzel百分比抑制率stop_spam] @ host.topdomain>在消息中写道
新闻:uH ************** @ TK2MSFTNGP09.phx.gbl ...

一点背景:

我使用三个处于子父关系的Datagrids。

我在DataTables上使用负自动增量,并且那就是工作


很好。


我的问题是当我更新这些网格并写入数据库时​​,我将新的主键和相关字段设置为Access中的新签名atuonumbers。

以下Sub是TopGrid适配器的RowUpdated,
将获得

新ID,然后将其设置为Datatable中的ParentID为


下一个网格。



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



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

-

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


adpProjParts.RowUpdated


如果e.StatementType = StatementType.Insert那么

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


e.Command.Connection)


Dim OldId As Long = e.Row()(" PRP_ID") ''这是主键

Dim NewID As Long = cmd.ExecuteScalar''这是新的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到0步-1 -



.dvProjCodes_COPY.RowFilter = Nothing

m_bIsSaving = True



drvRowIter.Row(" prco_prpid")= NewID

下一页

m_bIsSaving = False

结束如果

End Sub



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

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

如果我调试这个Sub,everythign很好,我一切似乎都在步进时获得正确的ID。但是当我稍后检查数据库时,一个

下一个网格中的更多项目(我刚刚使用此Sub更改了父ID
) )已经得到了旧的负自动编号...和


当然Access中的关系丢失了。

我一直想弄清楚哪些行是"改变"回到
否定ID,我唯一看到的就是'甚至远程关闭,
就是数据网格中的Row通常是我将光标放在$ b $上b当我

触发Save(更新数据集)时。但是那个'鼻涕完全正确,

也好像我已经选择了HAD的行然后左边也是



获取负面身份证......

但是......这是奇怪的部分..我的行有什么不重要

<在网格中选择了

..因为RowUpdated发生在所有这些之后......所以我现在完全感到困惑。

对我来说看起来即使RowUpdated Sub工作正常


当单步执行时,我设置
新的ParentID的DataTable中的一些项目也会改变回来之后那...

我知道很难解释这个问题...

请帮助!!!

/ Lars Netzel






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




解决方案

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




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






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






这篇关于更新数据集时出现一个非常奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆