View Single Post
 
Old December 7th, 2010, 03:47 AM
comecme comecme is offline
Registered User
 
Join Date: Nov 2010
Location: Noardburgum, Friesland, The Netherlands
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default Ch 14 adapter.Fill and ExecuteNonQuery

Hi DrPurdum,

In Listing 14-9 in the book, in method DoQuery(), you have the following code:
Code:
try
{
 ds = newDataSet(); // Instantiate DataSet object
 conn.Open();
 command.CommandText = txtQuery.Text;
 adapter = newOleDbDataAdapter(command);
 adapter.Fill(ds);
 dataGridView1.DataSource = ds.Tables[0];
 command.ExecuteNonQuery();
}
catch (Exception ex)
{
 MessageBox.Show("Error: " + ex.Message);
}
finally
{
 conn.Close();
}
I wonder why you use the command.ExecuteNonQuery() statement. As far as I can tell, the adapter.Fill(ds) already executes the SQL statement and fills the dataset.

In the book, you say that the call to Fill just passes the dataset and the ExecuteNonQuery executes the actual query. That is not what I see when debugging the code. Once the Fill has been executed, the DataSet contains a table with several rows in it.