views:

200

answers:

6

I have a data adapter. When I check the function at runtime I see the changes, but nothing happens in the database. How do I debug this? What went wrong?

OleDbDataAdapter adapter = new OleDbDataAdapter();
string queryString = "SELECT * FROM tasks";
OleDbConnection connection = new OleDbConnection(cn.ConnectionString);
adapter.SelectCommand = new OleDbCommand(queryString, connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

connection.Open();

adapter.Fill(ds.Tables["Tasks"]);                      

adapter.Update(ds);

return ds;
+2  A: 

Nothing is happening in the database because you're running a SELECT query. What are you expecting to happen?

somacore
I used the command builder, I thought no need to write other command. If I am wrong please explain
sadboy
You're right, however, you must modify your dataset after (not before) it's filled with your adapter for the command builder to actually detect changes.
Steve Wortham
+1  A: 

Well unless you snipped a lot of code, you are not changing anything in the dataset per se.

What are you trying to achieve?

Here, you are selecting some data, filling the dataset with it, then putting back the unchanged dataset in the DB.

You should first change something in the dataset itself before calling adapter.Update(ds)

Cheers, Florian

Florian Doyon
the ds I get to this function is after change(I see it at runtime, when I debug)
sadboy
A: 

As far as I can see you've not actually changed any of the contents of the tasks table. When you call adapter.Fill you are populated the empty dataset with the records from the database. You are then calling adapter.Update without changing any records within the dataset. The update command only performs changes when the dataset > datatable > datarows are edited and marked as dirty.

Brian Scott
the ds is marked as dirty. when I do fill, does the ds is being populate again?the ds I get in this function is *after* changes
sadboy
A: 

you are only specifying the select command. you also need to specify the insert command... i.e: DataAdapter.InsertCommand = new OleDbCommand....

lomaxx
even if I use command builder?at first I did but got lost with all the parameter thing...... do you have sample?saw only of sqlserver, and its not the same like access
sadboy
No, this isn't necessary when you use the command builder. But... there are some things about the command builder that can trip you up. See my answer for details.
Steve Wortham
+1  A: 

You are selecting data via the SelectCommand. If you want to update data, you need to run the UpdateCommand.

Xaisoft
can you explain how do I do it?
sadboy
+1  A: 

I assume you didn't post the full source code, as it looks as though you're not modifying the dataset. However, I just tweaked your code a bit (see my comments).

Hopefully this will help...

string queryString = "SELECT * FROM tasks";
OleDbConnection connection = new OleDbConnection(cn.ConnectionString);
connection.Open(); // open connection first

SqlCommand cmd = new SqlCommand(queryString, connection);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); // use the cmd above when instantiating the adapter
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

adapter.Fill(ds.Tables["Tasks"]);                      

// Modify your dataset here.
// Don't call AcceptChanges() as this will prevent the update from working.

adapter.Update(ds);

connection.Close(); // Close connection before ending the function

return ds;

This should allow OleDbCommandBuilder to do its thing by automatically scripting the database updates.

Steve Wortham
the ds the function gets , already after changes.perhaps I don't need to do fill? I change the ds in other layer and I send the changes ds to this function. I looked at runtime and saw that the changes has been done in the ds
sadboy
You'd need to find a way to make the changes after the fill and before the update (where I've added that comment). That's the only way the DataAdapter, CommandBuilder, and Update magic will happen.
Steve Wortham
I think you don't need to do connection.open(), it will be handled by the data adapter automatically .. :)
Mahesh Velaga