473,587 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update records using SqlDataAdapter

Hi.

I writed a method that takes two DataTable with same schema
and return a third DataTable that:
- insert new rows from "compare" DataTable
- delete rows contained in "source" and not in "compare" DataTable
- update rows from "compare"

--------------------------------------
public static DataTable GetMergedTable( DataTable source, DataTable compare)
{
DataTable ret = new DataTable();
ret.Merge(sourc e);
ret.Merge(compa re);
ret.AcceptChang es();

for (int i = 0; i < ret.Rows.Count; i++) {
DataRow row = ret.Rows[i];

bool sourceFound = source.Rows.Fin d(row["id"]) != null;
bool compareFound = compare.Rows.Fi nd(row["id"]) != null;

//Row added
if ((!sourceFound) && (compareFound))
row.SetAdded();

//Row modified
if ((sourceFound) && (compareFound))
row.SetModified ();

//Row deleted
if ((sourceFound) && (!compareFound) )
row.Delete();
}

return ret;
}
--------------------------------------

When I execute an update with SqlDataAdapter. Update
rows are inserted and deleted correctly, but never updated.

What is the problem?

Thx! ;-)
Matteo Migliore.

Sep 30 '07 #1
4 4831
Matteo,

Acceptchanges says that you have updated them and the changes can be set as
done in the datatables.

Cor

Oct 1 '07 #2
Acceptchanges says that you have updated them and the changes can be
set as done in the datatables.
The problem is not in AcceptChanges because this method
only change the row state to Unchanged, but with the "for"
I set rows in correct status.

Read here:
http://www.devnewsgroups.net/group/m...opic16270.aspx

Thx! ;-)
Matteo Migliore.

Oct 1 '07 #3
I don't see in the message thread that it is solved.

By the way, this is just a copy of a Microsoft AdoNet newsgroup discussion.

http://groups.google.nl/group/micros...a346c8aad9c860

In my idea you should be able to do the GetChanges direct, which gives you
only a shallow copy and be able because of that to do the updates. A
acceptchanges does set all to unchanged, however after that it has updated
the changes on the old data, in fact it is SetChanges and AcceptChanges in
one. . For new and delete this is of course not the question.

Cor
Oct 2 '07 #4
In my idea you should be able to do the GetChanges direct, which
gives you only a shallow copy and be able because of that to do the
updates. A acceptchanges does set all to unchanged, however after
that it has updated the changes on the old data, in fact it is
SetChanges and AcceptChanges in one. . For new and delete this is of
course not the question.
Thank you, but I don't understand how to solve this problem :-).

I need to compare two DataTable, the first contains the old data
and the second contains the new data. How can I do?

Merge and GetChanges doesn't go beacuse the second DataTable
is readed from a XML file. So does not contain activity
on rows, they are all on Unchanged status, initially.

Thx! ;-)
Matteo Migliore.
Oct 2 '07 #5

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

Similar topics

7
1789
by: skyjoker1 | last post by:
I think what I'm trying to do is something relatively simple. I have created a DataTable using the SQLDataAdapter.Fill method: Dim da As New SqlDataAdapter("SELECT * FROM Trans", cn) Dim ds As New DataSet da.Fill(ds, "AllTrans") I am currently editing the table records individually by selecting a subset of the data using a DataView and...
0
1268
by: Hugo Lefevre | last post by:
Dear, I have on a form a datagrid (datagrid1) which is filled by a dataset created in the class Afwezigheid. A button "Alles" show the whole table in the datagrid and when someone wants to change the data, I have to use a sqldataadapter which has an Update method. But how can I link this all ? Thank you very much, Hugo Lefevre
1
2989
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The problem come up when I want to delete one of rows in datagrid and update the change to the corresponding table. The error msg show up "Dynamic SQL...
4
3800
by: Oscar Thornell | last post by:
Hi, I have a relativley large/complex typed dataset that contains 7-8 tables and some supporting relational tables (lookups) for many-to-many relations. A good exampel would be a dataset that contained the whole Northwind database with all tables and relations. In my business logic I will retrive a populated instance of the dataset and...
4
2261
by: steroche | last post by:
I would REALLY appreciate help please please please! Im sure it is probably blindingly obvious to most of you but I am totally in the dark here!I am lost - i thought i had finally figured out this dataSet updating lark when i realised that i think i am right back at square 1!!! Here's my scenario - i have a SQLDB and i retrieve all my data...
2
1899
by: Calvin KD | last post by:
Hi everyone, I know the solution to my problem is probably very simple but I just can't seem to figure out how to do it. My problem is, I have an XML file as a datastore, I read it into a dataset for data manipulation and update the dataset with some edited data from the screen. However, I can't seem to make the changes "stick" in the...
3
1394
by: Antonio | last post by:
Can somebody tell my why the following procedure changes the data in the fields being updated to all the records in the database? private void updateRow(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { SqlConnection conn = new SqlConnection (ConfigurationSettings.AppSettings); SqlCommand updCommand = new...
2
1382
by: Antonio | last post by:
Can someone tell me why the following procedure updates ALL the records in the database with the field being updated for one record? private void updateRow(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { SqlConnection conn = new SqlConnection (ConfigurationSettings.AppSettings); SqlCommand updCommand = new...
1
1985
by: TonyJ | last post by:
Hello! I trying to update a table using sql server but no update will be done. I don't get any error at all. Here is an extract from the code. Does this seems to be right.
1
3490
by: gaya12 | last post by:
I have a table called Student in a SQL databse.. I have to create a simple windows application in C# to Retrieve that table for the following functions.. 1.Add Data 2.Update Data 3.View Data For this Im using Datagridview and ADO.NET.. Im able to view the data.. but I dont know how to Add and update data... When I add/update the changes...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.