Local database inserts not being stored - c#

This is probably a stupid mistake of me.. but I can't seem to understand it.
I've created a new, empty C# Windows Forms application.
I added a Database (Based on a dataset) and have the file stored in my solution explorer.
I've added a table Test with column Name.
I add a record using new SqlCeCommand("insert into Test values('Name')", new SqlCeConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)).ExecuteNonQuery();
However, I've even tried retrieving the same data and it all works perfectly.
But when I stop my project, and rebuild it.. all data is gone again?
Is there a way to fix this, or will this fix itself when I'll start using this project for what it is intended (Without the 500 rebuilds a day?)

Your database file is listed in the project with this property
Copy to destination directory = Copy Always
If this is true then every time you restart the project a fresh (empty) copy of the database file is copied from the project directory to the output directory (BIN\DEBUG or BIN\RELEASE) overwriting the database file used in the previous run. You could avoid this changing the property to Copy Never or Copy if newer

The answer given by steve keeps copying the database over the existing one, which results in removing all data.
I've managed to fix this by putting "Copy Always" on, then in the explorer move the database to a different location and add it to the project. This way the database will never be overwritten and can be used in the program!
(However, this will probably raise a issue if/when I publish the project to another computer)

Related

Change Include Database In Project Setting After Creating Dataset

When you create a dataset, you get the option of including the database file in your project, instead of the connection string pointing to the database file in the sql server data folder. My question is, is you select no, how can you later, after creating the dataset, change your mind and change, that the database file should be included in your project, and should be included in the release folder
You can use the same option as you would to include any other existing item in your project.
You can then change the connection string in the Settings for the project, but I'm not sure whether that will work at design time or only run time. You can easily enough just delete the DataSet and regenerate the Data Source.
The data file will be included in the output folder by default, but you should change the Copy to Output Directory property to Copy If Newer unless you want to lose any changes you made during testing each time you build.

Publishing website solution - How to stop a deleted file from being regenerated?

I have a website solution in Visual Web Developer. I have replaced a new file with an older file of the same name. I understand that the default behavior for web deploy is to update only older files with new ones. However, in this case I have replaced the new one with the old one by right clicking it (the old one) and clicking 'publish myfile'. This did in fact save the old file as I want.
The problem begins when I later publish the whole website again (to update other files). Somehow - this older file, which I want on the server, is replaced with the newer version - the one that at least theoretically, doesn't exist neither in my files, nor on the server! Where does it come from? And how can I stop that from happening?
I've found a solution, though not why the problem exists.
Open the folder and make a copy of the file. Then, in solution explorer - delete the original. Publish. Rename the copy to the original name. Publish again.
There may be unnecessary steps here. I'm just posting something that worked.

Files show as hidden in Visual Studio 2012

Hei,
So we have a whole group of people working on the same project and every now and then some files show as hidden, you know the blank file icon like obj or bin.
See my font and texture? They should be normal.
So, I can see them in the folder but not in solution explorer without hitting the Show Hidden Files. I can also modify them but I would like them back.
I have been trying to see if there was a solution but all I get is blogs on how to show hidden files.
So I found this thread
Visual Studio 2012 and missing files
that seems to be giving the reason of my problem. People are not saving properly before pushing to github.
Now is there a solution to this?(apart from saving properly) Can I make all of those files become normal files?
Cheers
Simply right click the files/folders and include them to source control again.
This usually happens if people check in new files/folders but do NOT check in the updated project file, too. The project file contains a list of all files which should be included within the solution. If this runs out of sync, exactly this crap will happen... ;)
Simply instruct your folks to properly check-in. Or do code review on check-in and verify that the project/solution file(s) are also checked in.
They show up as hidden because they're not part of the project/solution, but they are on your file system.
You can right-click the files and click Include in Project
If this does not solve the issue after checking in. The project file is what is not properly getting checked in.
Probably the problem is that they are not pushing the project file (file with extension .csproj). It holds all references to files included on the project so it needs to be pushed when new files are added.
Let everyone know that they need to push this file in order to everyone to catch latest changes on project.

Data lost from .mdf file as I exit application

Recently I work on SQL Server 2008 R2 database. I create database and attach .mdf file of same database in my application with some default data in it . Run application default data coming properly. Now I insert, update some data in my application and its works fine. But as I exit application and again run application lastly added and updated data get lost but default data coming proper as earlier. Please help. Why new
As mention by #Henk , #Microtechie , I scan my project folder and found there are 3 copies of .mdf file are there, 1st in project folder where code project([ProjectFolder ]) resides 2nd in [ProjectFolder]/bin/debug folder and 3rd in [ProjectFolder]/bin/release folder and suddenly solution to my que trigger in my mind. Problem not in multiple .mdf files in project folder, as I every time ‘Clean’ and ‘Build’ my solution new copy of .mdf file from [ProjectFolder] get copied into [ProjectFolder]/bin/debug folder, result in override of last .mdf file in same folder. Hence every time I build and run application only default data coming and last added and updated data get lost.
Thanku all for your replies and precise answer..!
What kind of object do you load your .mdf file data into? It is likely that you need to save the changes in that object before closing the application.

copy back database

i've added my database to my source folder and whenever i launch the program it copies the db in my Debug folder but the modifications don't seem to be copied back in my source folder,over the old DB .
How can i do so automatically ?
Since there is no "after execution" script being called when the application dies and you are returned to Visual Studio (as I assume is the case you are talking about) you can create a pre-build script which checks if the DB in the DEBUG folder is newer than the source, and then copies it back before the building.
This way you will get the changes back, although you would be one version behind.
A better solution is to check that the DB is not copied to the output folder, and reference it with an absolute path instead of a relative one. This way you would always work against your live database. (I assume you are using SQL Express)
Three ways:
Put an XCopy command in your Pre-Build event (project->Properties->Build Events). But this will only do it each time you do a build, not after the program has run.
Add code to your program to "back-up" a database to a path you specify in App.config, where that path is your source folder.
Launch your program from a .bat file that copies the DB back to wherever you want when the program exits.

Categories

Resources