Easy question about a not working tutorial about ado.net

T

TonyJ

Hello!

I use VS2005 and C#.
I'm looking at ADO.NET and have found some test tutorial on
microsoft MSDN.

It can be found on google when searching for "Walkthrough: Saving Data to a
Database (Single Table)"
You must have access to the Northwind sample database.

I have followed the walkthrought in this tutorial exectly but it doesn't
save data back to the worthwind database as the tutorial say.

The tutorial say the following.
1. Create a project

2. Create a Data Source by using the Northwind database table customer

3. To set the controls for the items in the Data Sources window.
Expand the Customers node in the Data Sources window.
Change the control for the Customers table to individual controls by
selecting Details from the drop-down list on the Customers node.

4. Create data-bound controls on the form
Drag the main Customers node from the Data Sources window onto
Form1.

5. Modifying the Code to Update the Database
You can update the database by calling the Update method of the
CustomersTableAdapter. By default, an event handler for the
BindingNavigator's Save button is added to the form's code to send
updates to the database. This procedure modifies the code to include
error
handling by wrapping the update call in a try-catch block.
You can modify the code to suit the needs of your application.

6. To add update logic to the application
Double-click the Save button on the BindingNavigator to open the
Code Editor to the bindingNavigatorSaveItem_Click event handler.
Replace the code in the event handler to add some error handling.
The code should look like the following:
try
{
this.Validate();
this.customersBindingSource.EndEdit();
this.customersTableAdapter.Update(this.northwindDataSet.Customers);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
}

7. Testing the Application
Press F5.
Note here Update successful is written so everything seems normal.
Make some changes to the data of one or more records.
Press the Save button.(It's a diskett icon)
Check the values in the database to verify that the changes were saved.

So I do the following change some data in one of the control(text field)
and then click the diskett
icon meaning I saving the data back to the database. If I now go to the next
record and then go back to the previous record the change is there. So I
manage to change in the dataset but if I then close the application and look
in the database my changes has not changed anything in the database.I can't
understand why this doesn't work.
Is this a bug perhaps. I would be very glad if someone have any comment
about my problem.
Is it possible to trace in some way why this row
this.customersTableAdapter.Update(this.northwindDataSet.Customers);
doesn't update the customer table in the northwind database.

//Tony
 
G

Guest

Hi Tony

I've just ran through the steps you've listed (also found here
http://msdn2.microsoft.com/en-us/library/0f92s97z(VS.80).aspx) and I've had
no problems.

Are you sure that the database you connect to when checking the changed
values, is the same one you connected to from Visual Studio ?
(You can check the database settings in the App.Config file)
That's the only reason I can think of that would produce the results you are
seeing.
 
T

TonyJ

Hello!

I found my problem.

When I created the connection to the database I was asked if I wanted to
copy the database file to my project.
Here I answered Yes. The default settings for the copied database into my
project was copy always.
Copy always mean that a copy is made from the project root directory to the
output bin directory everytime you run the program.
This means that I really changed my database in the output bin dirctory but
when I ran the applictiopn for the second time the database from the project
root directory was copied to my output bin directory overwriting my changes.

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top