Wrox Programmer Forums
|
BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C#
This is the forum to discuss the Wrox book MCSD Certification Toolkit (Exam 70-483): Programming in C# by Tiberiu Covaci, Rod Stephens, Vincent Varallo, Gerry O'Brien; ISBN: 978-1-118-61209-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 6th, 2017, 07:33 AM
Registered User
 
Join Date: Jan 2017
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 9 use a dataadapter to add a record

Codelab on p385 and p386 is below.
The data from the person table is extracted into a DataSet obj via the Fill command. DS at this point would contain all the records of the person table
da.Fill(ds, "Person");

We then add a new row to the DS.
DataRow newRow = ds.Tables[0].NewRow();
newRow["FirstName"] = "Jack";
newRow["Surname"] = "Johnson";
ds.Tables[0].Rows.Add(newRow);

My question is a confirmation. i assume ds.tables[0].Rows.Add(newRow) must clear the contents of the DS and add one row (jane doe). As the update command da.update(ds.TAbles[0]) inserts all of the rows in DS to the database.
CAn you please confirm this or explain otherwise. i am failing to understand how the DS suddenly contains all the data and then after those adding row statements it is reduced to a single record.
 
Old January 7th, 2017, 01:58 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Ksackel. Sorry for the slow reply. I was hoping the author of that chapter would respond (so I would have to think ).

The short answer is no, when you add a new record to it the DataSet still holds all of the other records.

Here's the longish explanation.

When you add a new record to a DataSet, the old records are still in it. Each rows has a state that records whether it is the original data loaded from the database, modified, added, or deleted.

You can see the records' statuses by looking at the row's State property. For example:

ds.Tables[0].Rows[5].RowState

When you add the new record, it is marked as new.

When you use Update to save changes to the database, the data adapter copies any changes (adds, deletes, or changes) back to the database.

It doesn't touch the unmodified records because they haven't changed. That can save a ton of time. For example, suppose you have 5,000 records but you only changed 1.

It then changes the state of all of the modified records to Unchanged to show that the data set is up to date with the database.

I hope that helps. Reply if it doesn't make sense.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find Last Record and Add new keyed record causualuser Access VBA 3 February 9th, 2009 08:24 AM
how to add new record as first record in dataset [email protected] ASP.NET 1.0 and 1.1 Professional 4 April 21st, 2006 05:23 AM
Help with Add record topshed Classic ASP Basics 6 February 7th, 2006 02:59 AM
Add row using dataadapter.update JelfMaria VS.NET 2002/2003 0 June 29th, 2005 09:06 AM
Cannot add new record geolando Access 0 June 27th, 2005 06:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.