.NETGURU
dataset incorrect about HasChanges
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngdata' list.


Andy Smith
ok, I have a dataset of new,ready-to-insert relational data in several =
tables.
My code to insert it runs thru the dataset and calls adapter.Update on =
each table in a parent-first fashion.
the first table is inserting correctly, and my keys are updating and =
cascading correctly... but after the first run thru the loop, the =
dataset claims there are no more changes when every other table has =
non-inserted data still! What's going on? Why does the dataset think =
that there's no more changes? How can I insert the rest of the data?

I'm using the following code to attempt to insert the data into the =
database:

Public Sub Save()
'save the package to the database
Dim cn As New SqlConnection(DataUtils.ConnectionString)
Dim da As SqlDataAdapter
Dim cb As SqlCommandBuilder

' A is the parent of B is the parent of C
Dim tableSaveOrder As New ArrayList()
tableSaveOrder.Add("A")
tableSaveOrder.Add("B")
tableSaveOrder.Add("C")
Dim currentTable As Object

cn.Open()

For Each currentTable In tableSaveOrder
da =3D New SqlDataAdapter("Select * From " & =
currentTable.ToString(), cn)
AddHandler da.RowUpdated, AddressOf MyRowUpdatedEventHandler
cb =3D New SqlCommandBuilder(da)
Debug.WriteLine(mPackage.HasChanges()) <!---- =
This is false after the first table!
da.Update(Me.Package, currentTable.ToString())
Next
=20
cn.Close()

End Sub

Public Sub MyRowUpdatedEventHandler( _
ByVal sender As Object, _
ByVal e As SqlRowUpdatedEventArgs _
)
Dim da As SqlDataAdapter =3D CType(sender, SqlDataAdapter)
Dim cn As SqlConnection =3D da.SelectCommand.Connection
Dim cmd As New SqlCommand("Select @@Identity", cn)
e.Row.Item(e.Row.Table.PrimaryKey(0).ColumnName) =3D =
CType(cmd.ExecuteScalar(), Int32)
e.Row.AcceptChanges()
End Sub

__
Andy Smith
Chief Code Monkey

Reply to this message...
 
    
Andy Smith
ok, I found out what the problem is, and i'm giving the answer here so =
other's don't waste half a day looking for it like I did.

In the code for a typed dataset that you plan on inserting in a batch =
job like this... make sure that the ForeignKeyConstraint's for the =
relationships have their AcceptRejectRule to None. Otherwise, when you =
update the parent record, all the child records will have their =
AcceptChanges method called.

__
Andy Smith
Chief Code Monkey

-----Original Message-----
From: Andy Smith=20
Sent: Friday, May 24, 2002 3:55 PM
To: aspngdata
Subject: [aspngdata] dataset incorrect about HasChanges

ok, I have a dataset of new,ready-to-insert relational data in several =
tables.
My code to insert it runs thru the dataset and calls adapter.Update on =
each table in a parent-first fashion.
the first table is inserting correctly, and my keys are updating and =
cascading correctly... but after the first run thru the loop, the =
dataset claims there are no more changes when every other table has =
non-inserted data still! What's going on? Why does the dataset think =
that there's no more changes? How can I insert the rest of the data?

I'm using the following code to attempt to insert the data into the =
database:

Public Sub Save()
'save the package to the database
Dim cn As New SqlConnection(DataUtils.ConnectionString)
Dim da As SqlDataAdapter
Dim cb As SqlCommandBuilder

' A is the parent of B is the parent of C
Dim tableSaveOrder As New ArrayList()
tableSaveOrder.Add("A")
tableSaveOrder.Add("B")
tableSaveOrder.Add("C")
Dim currentTable As Object

cn.Open()

For Each currentTable In tableSaveOrder
da =3D New SqlDataAdapter("Select * From " & =
currentTable.ToString(), cn)
AddHandler da.RowUpdated, AddressOf MyRowUpdatedEventHandler
cb =3D New SqlCommandBuilder(da)
Debug.WriteLine(mPackage.HasChanges()) <!---- =
This is false after the first table!
da.Update(Me.Package, currentTable.ToString())
Next
=20
cn.Close()

End Sub

Public Sub MyRowUpdatedEventHandler( _
ByVal sender As Object, _
ByVal e As SqlRowUpdatedEventArgs _
)
Dim da As SqlDataAdapter =3D CType(sender, SqlDataAdapter)
Dim cn As SqlConnection =3D da.SelectCommand.Connection
Dim cmd As New SqlCommand("Select @@Identity", cn)
e.Row.Item(e.Row.Table.PrimaryKey(0).ColumnName) =3D =
CType(cmd.ExecuteScalar(), Int32)
e.Row.AcceptChanges()
End Sub

__
Andy Smith
Chief Code Monkey

| [aspngdata] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngdata.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
 
System.Collections.ArrayList
System.Data.AcceptRejectRule
System.Data.ForeignKeyConstraint
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlCommandBuilder
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlDataAdapter
System.Data.SqlClient.SqlRowUpdatedEventArgs
System.Diagnostics.Debug




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification