Need to trick RowState

  • Thread starter Thread starter Wes Graves
  • Start date Start date
W

Wes Graves

Hi Everyone,

We have a problem that requires us to work around the RowState
property of a DataRow.

We have a DataTable in memory that we have modified certain rows and
columns. Those rows were initially retreived from the database and as
such now have their RowState set to Modified. We have some common
code that runs checking the RowState to see if it's Modified or Added
and takes the appropriate action. In this special situation we need
the rows that have been modified to actually be inserted(Added) not
updated(Modified).

The situation occurs because we are doing a New From Current (NFC) in
our application. A NFC simply "copies" the data of one "application"
to another so that the user doesn't have to enter all the same data in
again. Behind the scenes, this copy simply clears out some fields
that can't be carried over and resets a boolean flag.

Any ideas how we can get around this? Let me know if you need more
details.

Thanks,

Wes
 
Hi,
There are many ways to handle your problem, but you will have to chose the
best based on counts of performance or ease of coding etc. Here are two
approaches I can think of:

1. When you import the data, you just clear of certain fields in the
DataTable, hence the RowState is modified right. You can work around this by
actually giving a command object used for inserting data as the
UpdateCommand of the DataAdapter. So, when you call upon dataadapter.update,
the DataAdapter invokes the command object set for UpdateCommand property
and this in turn calls the SQL for Inserting data into the database. Note
that you can have the Update and Insert commands can call upon a common SP
which can do either insert or update based on the boolean flag (set by the
import)

2. The other way, (not so good) is to create new DataRows (using
DataTable.NewRow) when you import data, such that the RowState is "Added".
After adding the new rows, you can remove the old rows using
DataTable.Remove ( not DataTable. Delete!)
 
If you have 50 rows, is there a shorter way to do this and still use the
CommandBuilder so you don't have to write all the code to handle the insert?


Manoj G said:
Hi,
There are many ways to handle your problem, but you will have to chose the
best based on counts of performance or ease of coding etc. Here are two
approaches I can think of:

1. When you import the data, you just clear of certain fields in the
DataTable, hence the RowState is modified right. You can work around this by
actually giving a command object used for inserting data as the
UpdateCommand of the DataAdapter. So, when you call upon dataadapter.update,
the DataAdapter invokes the command object set for UpdateCommand property
and this in turn calls the SQL for Inserting data into the database. Note
that you can have the Update and Insert commands can call upon a common SP
which can do either insert or update based on the boolean flag (set by the
import)

2. The other way, (not so good) is to create new DataRows (using
DataTable.NewRow) when you import data, such that the RowState is "Added".
After adding the new rows, you can remove the old rows using
DataTable.Remove ( not DataTable. Delete!)

--
HTH,
Manoj G [.NET MVP]
http://www15.brinkster.com/manoj4dotnet


Wes Graves said:
Hi Everyone,

We have a problem that requires us to work around the RowState
property of a DataRow.

We have a DataTable in memory that we have modified certain rows and
columns. Those rows were initially retreived from the database and as
such now have their RowState set to Modified. We have some common
code that runs checking the RowState to see if it's Modified or Added
and takes the appropriate action. In this special situation we need
the rows that have been modified to actually be inserted(Added) not
updated(Modified).

The situation occurs because we are doing a New From Current (NFC) in
our application. A NFC simply "copies" the data of one "application"
to another so that the user doesn't have to enter all the same data in
again. Behind the scenes, this copy simply clears out some fields
that can't be carried over and resets a boolean flag.

Any ideas how we can get around this? Let me know if you need more
details.

Thanks,

Wes
 
I had a similar problem.
It would be nice to simply change the property from Modified to Added but
the darn thing is read only.

I went with the 2nd set of Data Rows routine.
I loaded the first dataset with some data but did not know if it was to be
added or updated in the database.

Then I pulled in a 2nd dataset from the database the contained the "current
values".

Then I looped through the first set of rows, checked the 2nd set for an
existing value, and then modifed or inserted data as required. Then the row
state of the 2nd set was correct and I could pass it back to the database
through a dataadapter.

The code was ugly though. What made it worse was the existence of a parent
child realtionship so I had to duplicate it for the child objects!

There has to be a better way...
 
Back
Top