Which line of code should you insert at line 09?

The application populates a DataSet object by using a SqlDataAdapter object.
You use the DataSet object to update the Categories database table in the database. You write the following code segment.
(Line numbers are included for reference only.)

01 SqlDataAdapter dataAdpater = new SqlDataAdapter(“SELECT CategoryID, CategoryName FROM Categories”, connection);
02 SqlCommandBuilder builder = new SqlCommandBuilder(dataAdpater);
03 DataSet ds = new DataSet();
04 dataAdpater.Fill(ds);
05 foreach (DataRow categoryRow in ds.Tables[0].Rows)
06 {
07 if (string.Compare(categoryRow[“CategoryName”].ToString(), searchValue, true) == 0)
08 {
09 …
10 }
11 }
12 dataAdpater.Update(ds);

You need to remove all the records from the Categories database table that match the value of the searchValue variable.
Which line of code should you insert at line 09?

The application populates a DataSet object by using a SqlDataAdapter object.
You use the DataSet object to update the Categories database table in the database. You write the following code segment.
(Line numbers are included for reference only.)

01 SqlDataAdapter dataAdpater = new SqlDataAdapter(“SELECT CategoryID, CategoryName FROM Categories”, connection);
02 SqlCommandBuilder builder = new SqlCommandBuilder(dataAdpater);
03 DataSet ds = new DataSet();
04 dataAdpater.Fill(ds);
05 foreach (DataRow categoryRow in ds.Tables[0].Rows)
06 {
07 if (string.Compare(categoryRow[“CategoryName”].ToString(), searchValue, true) == 0)
08 {
09 …
10 }
11 }
12 dataAdpater.Update(ds);

You need to remove all the records from the Categories database table that match the value of the searchValue variable.
Which line of code should you insert at line 09?

A.
categoryRow.Delete();

B.
ds.Tables[0].Rows.RemoveAt(0);

C.
ds.Tables[0].Rows.Remove(categoryRow);

D.
ds.Tables[0].Rows[categoryRow.GetHashCode()].Delete();

Explanation:
DataRow Class
(http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx)

DataRow.Delete() Deletes the DataRow.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Gaius

Gaius

Any action that modifies the ds.Tables[0].Rows collection will not compile within the foreach loop. Additionally, you do not want to remove the row from the data set, only update its state to “deleted” in order for the data adapter to know what to do. If you remove the row from the data set, it will become detached, and the dataAdapter.Update() call will do nothing.

John Galt

John Galt

Just confirming the correct answer is A, because as Gauis points out, you can’t modify the Rows collection (or any other collection for that matter), while iterating through it.

Then again, look at Microsoft documentation here:
http://msdn.microsoft.com/en-us/library/system.data.datarow.delete.aspx

“Delete should not be called in a foreach loop while iterating through a DataRowCollection object. Delete modifies the state of the collection.”

Still, I’ll go with A on the actual exam.