473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

SqlDataAdapter, DataSets and deleting rows

I'm delving into using ADO.NET for the first time. In fact, I've never
done any database work using C# at all so all of this is new to me.
Please bear that in mind if I am asking stupid questions.

In reading the documentation for the "Update()" method of the
SqlDataAdapter class, I see that it "Calls the respective INSERT, UPDATE,
or DELETE statements for each inserted, updated or deleted rows in the
specified array of DataRow objects". I've got the INSERT and UPDATE
parts working great but I'm having problems with the DELETE part. It
doesn't
matter how many rows I delete from the DataSet, when I call the Update()
method, those rows are not deleted. Why? How does the Update() method
know which rows have been deleted so that it knows which rows would need
to be deleted from the database?

Here's the sample code I'm using. Please note that this is just a test
method
created so that I can just see it working. This is by no means meant to be
efficient or do anything meaningful.

private void doUpdateSQL() {

DataRow oDR;

try {

SqlConnection myConn = getSqlConnection();

SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.SelectCommand = new SqlCommand("SELECT * FROM
Employees", myConn);
SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);

myConn.Open();

DataSet ds = new DataSet();
myDataAdapter.Fill(ds, "Employees");

// Removing last record

ds.Tables["Employees"].Rows.Remove(ds.Tables["Employees"].Rows[ds.Tables["Em
ployees"].Rows.Count-1]);

myDataAdapter.Update(ds);
ds.AcceptChanges();

//code to modify data in DataSet here
//String SearchID = "1";
oDR = ds.Tables["Employees"].Rows[0]; //.Find(SearchID);

oDR.BeginEdit();
oDR["FirstName"] = "Anne";
oDR["LastName"] = "Davolio";
oDR.EndEdit();

//Without the SqlCommandBuilder this line would fail
myDataAdapter.Update(ds, "Employees");

DataRow newDR = ds.Tables["Employees"].NewRow();
newDR.BeginEdit();
newDR["FirstName"] = "New";
newDR["LastName"] = "User";
newDR.EndEdit();

ds.Tables["Employees"].Rows.Add(newDR);

myDataAdapter.Update(ds, "Employees" );

myConn.Close();

} catch( Exception oErr ) {
tbSQLOutput.Text += "Error:\r\n" + oErr.Message;

}
}

Any pointers to what I might be missing or misunderstanding would
be very much appreciated!!

thnx,
Christoph
Nov 16 '05 #1
3 12236
Christoph <jc*****@yahoo.com> wrote:
I'm delving into using ADO.NET for the first time. In fact, I've never
done any database work using C# at all so all of this is new to me.
Please bear that in mind if I am asking stupid questions.

In reading the documentation for the "Update()" method of the
SqlDataAdapter class, I see that it "Calls the respective INSERT, UPDATE,
or DELETE statements for each inserted, updated or deleted rows in the
specified array of DataRow objects". I've got the INSERT and UPDATE
parts working great but I'm having problems with the DELETE part. It
doesn't
matter how many rows I delete from the DataSet, when I call the Update()
method, those rows are not deleted. Why? How does the Update() method
know which rows have been deleted so that it knows which rows would need
to be deleted from the database?


Because the rows which have been deleted are still meant to be in the
DataTable, just with a state of Deleted.

The problem you've got is that you're calling Remove on the
DataRowCollection rather than just calling Delete on the row itself.
You also shouldn't call AcceptChanges yourself - the adapter will do
that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
> Because the rows which have been deleted are still meant to be in the
DataTable, just with a state of Deleted.
When I query the table, those rows are still being returned as part of the
result set. Should I see that they have a different state?
Also, does this mean that if I want them physically deleted (such that they
do not get returned as part of the result set), I'll need to manually delete
them myself with a DELETE query?
The problem you've got is that you're calling Remove on the
DataRowCollection rather than just calling Delete on the row itself.
Only because I wasn't aware of that method. Shouldn't both methods to
basically the same thing?
You also shouldn't call AcceptChanges yourself - the adapter will do
that.


Noted. Thanks.

Chris
Nov 16 '05 #3
Christoph <jc*****@yahoo.com> wrote:
Because the rows which have been deleted are still meant to be in the
DataTable, just with a state of Deleted.
When I query the table, those rows are still being returned as part of the
result set. Should I see that they have a different state?


You need to be very careful with terminology here - note that I said
they're meant to be in the DataTable, not in the original database
table. What did you mean when you wrote "When I query the table"?
Also, does this mean that if I want them physically deleted (such that they
do not get returned as part of the result set), I'll need to manually delete
them myself with a DELETE query?
The problem you've got is that you're calling Remove on the
DataRowCollection rather than just calling Delete on the row itself.


Only because I wasn't aware of that method. Shouldn't both methods to
basically the same thing?


No. Remove removes it from the DataTable completely; Delete marks it
for deletion so the DataAdapter knows to execute a delete command.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

6
by: Mark | last post by:
I get an error message when deleting rows from a table in Access database. My understanding is that the error message relates to the sharing of the Inetpub and the wwwroot directory. While I...
1
by: Junkguy | last post by:
I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following. I'm trying to delete all the rows in a dataview. There are 200 rows. Everything works fine until I delete half way through and then I'm...
3
by: marshallarts | last post by:
Hello, I have a datagrid (grdShots) on a form, and a button to allow the user to delete records from the dataset underlying the grid. My code appears to work, because the row disappears from...
5
by: A | last post by:
I am wondering if there's an efficient way of deleting rows or columns of a gsl_matrix? The only way I can think of is copying individual elements or may be rows, columns or sumatrices to a new...
8
by: NAdir | last post by:
Hi, thank you for your help. My VB.Net application contains a document that the user can refresh at any time. The refresh works fine and needs to loop through few datatables (hundreds of rows)....
11
by: Greg | last post by:
I have a gridview on my form which I have populated using a datareader. What I would like to do is to be able to remove specified records from the gridview, without affecting the source of the...
2
by: karthick | last post by:
Hi, I am reiterating through a Datatable and deleting few rows based on the user's selection (with the index ref). I use datatable.rows.Delete() for this purpose. But if I select three rows, it...
1
by: azegurb | last post by:
hi all i have taken from internet a script that adds and removes rows but i made a little changes to that script that is renumbering rows after deleting. but one problem arises. i can delete all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.