WinForms – Database Changes not sticking.

Saturday afternoon I spent trying to get a Windows Forms application to add data to a database.  It all seemed to work correctly.  Add changes, no errors.  Close window, open window.  Yep changes are there.  Close application, re-run application.  Yep, changes still there.  Make a change to the code, re-run.  No data.

This happened with either an SQL database that was in my project or an Access database that was in my project.

Further if I checked the database via visual studio, it looked like the data never got copied into the database that was in my project.

I did a search all over google and came up with nothing.  And now that I’ve discovered what the answer is, I remember that I had this exact same issue several month ago when I first started using Visual Studio 2008 on another Windows Forms application.  So, this post is first, to help me remember and second, to help anyone else who might be having this problem.

When Visual Studio compiles the application, it copies what ever database you have in the project into the directory that the exe is in.  This means that what ever data you have in the database at the project level is what you will see when you run the application.

My solution to this behavior was to change the connection string in app.config so that the database being used is the one in the project and not the one in the exe directory.

Hope this helps someone.

Related Post

Comments are closed.