I have a simple question. I'm new to .net and sql and I'm trying to write a small form application. I added a data source which I created in sql. I got textboxes, button to update tables in database. When I made changes in database, like adding or deleting rows from tables, these updates are getting lost if I made a little change in application code (for example entering a new line).
Probably I'm unaware of something simple. What is the cause of it?
Select the database from solution explorer, go to properties (or alt + enter) and change the Copy to Output Directory to Copy if newer (copy if you change the database) or Do not copy. If the selected option is Copy always, the database is copied each time the project is build.
Related
I'm trying to create a recipe book in C# where I can easily search for what recipes I can make with what ingredients are on hand. This is mostly a learning exercise for me at this point.
Right now, I'm trying to test updating the tables in my database. It seems to work during one instance of the code, but the changes are gone the next time I run it.
So I have a table in my database like this:
I create a small list of example recipes from a json file
var js = new JsonImporter() { SourceDirectory = Path.Combine(Directory.GetCurrentDirectory(), "test_data") };
List<Recipe> recipes = js.ImportData<Recipe>("test_recipes.json");
Connect to my table and InsertAllOnSubmit:
DataContext db = new DataContext(connectionString);
Table<Recipe> db_recipes = db.GetTable<Recipe>();
db_recipes.InsertAllOnSubmit(recipes);
Show how many entries are in the table, submit the changes, refresh the number of entries, and show how many are in the table again:
MessageBox.Show(db_recipes.Count().ToString());
db.SubmitChanges();
db_recipes = db.GetTable<Recipe>();
MessageBox.Show(db_recipes.Count().ToString());
The first messagebox shows 1 (I have one row in the table that I entered manually via the Server Explorer), and the second message says 3. This is what I expect, since I am adding 2 entries.
However, once I run the code again, the message boxes again say 1 and 3, instead of always incrementing by 2. So it seems to me like the database is not saving. Can anybody help me figure out what's going on here?
You never said what database you use but I think it highly likely to be file based, either Access or SQL Server (in "attach the mdf file at runtime" mode)
Often with file based databases we end up setting them up so that there is a version of the db file in the project folder alongside the source code. This is copied to the output directory every time the project is built and the output directory contains the exe and the db. The exe modifies the db in the output folder. The next time it is built / run the blank db from the source code folder is copied over the top of the db that was modified in the output folder
Run your project, get the exe to modify the db, stop the exe and then search the whole project folder (using windows file search) or even the whole hard disk for the name of your db file. You'll figure out what's going on by looking at the file modified dates..
If it is a file based db thing, find the db in CS Solution explorer, click it and set its "copy to output" setting to "copy if newer" so that it only wipes the output db if eg the scheme has changed
I have a linq-to-sql connection for my database. I added a new table to the db in SQL server management studio today. Since it doesn't get automatically added in the dbml file, I dragged and dropped the table from server explorer as visual studio suggests.
This automatically makes the necessary changes to its designer.cs file, which is good.
Unfortunately, it also makes some other changes I didn't ask for. There are a bunch of variables getting renamed in the designer file consistently, which has nothing to do with the table I'm adding or the one it's connected to!
I am being very careful not to move any other table in that layout- I'm just doing a drag 'n drop in an empty area. I tried this multiple times with the same result, so maybe I'm doing something wrong. I searched everywhere and I can't seem to find a better way to update the dbml file for tables added ( I found a bunch of questions about updating the dbml file when columns are added to existing tables, unfortunately those answers don't apply here).
Has anyone ran into this issue before? Is there any solution to this?
The solution is to ignore it. The entire purpose of designer files is that they are tool-generated and may be regenerated at any time, for any reason, with any number of changes.
Under no circumstances should you modify them, and there's usually not much of a reason to look at them.
I had to fix it the hard way- manually updating the variable names by looking at the diff of the designer file. It was not fun, but it got rid of all the errors.
Apparently, it was renaming a bunch of associations for some other table consistently. Turns out someone had renamed those associations in the designer file instead of the .dbml file. So every time any change is made to the .dbml file, the designer file recompiles, and old names come back! I should probably update those associations in the dbml file so we don't have this problem in future.
I am creating a library application in Visual Studio 2010 Professional and Access 2010. I bind the Access database to Visual Studio. When I fill out the fields and click submit I can see the new record in the DataGridView, but when I close the application the new record is not saved and I have to input the record again.
Can somebody help me to know why when I input the new record through the application the record is not saved in the database?
It is a very common situation. You have your database file (the MDB or ACCDB file) listed between your project items. If you click on this file and look at the properties window you will see a property called Copy to the output directory. If this property is set to Copy Always then every time you start a debug session the database file listed in your project items is copied by VS in the output directory (usually BIN\DEBUG). Of course this copy doesn't contain the records inserted in your last debug session and you think that your previous insert has failed.
Setting this property to Copy If Newer, the mentioned behavior will happen only if you change the database schema manually.
Setting this property to Copy Never, will let you manually copy the database file.
In my project, I have a data-access layer that contains .dbml file(named test.dbml) used to drag and drop stored procedures and tables. Two days ago when I drag and drop the stored procedure onto my test.dbml file. Upon saving it creates another test.designer1.cs class and the old test.designer.cs class remains unchanged. I use visual studio 2010 and it was very weird for me. I've checked my rights on the folder which contains my project but all seems ok because two days earlier it was working fine and suddenly it happened. I Google it but find no solution so I decided to write this question.
At-last i've solved it myself.........and i am going to share it that what i have done. my dbml file name was test.dbml having test.dbml.layout and test.designer.cs files now the problem was when ever i drag and drop stored procedure or table it creates a new designer file with name test.desginer1.cs and when i build data-ccess layer it gives me hell of errors my brain was out at that time and with angry face i delete the test.designer.cs (original file) from the solution after that there was only one file test.designer1.cs which i renamed from test.designer1.cs to test.designer.cs and build data-access layer and i was wondered that it was build successfully. this solve my problem however there are other points as well to keep in mind
make sure your dbml files should not be read-only
you have full access to write file on disk (means check your rights on directory where your solution is placed)
I am trying to learn Monotouch and have an SQLite db with information.
Using the examples here I can open, fill and query the database (which is set as "Content" in my project), but when I rerun the program, no data exists in the database again.
I am guessing that somehow I have to save the database file to the Documents directory after changing it, but I can't quite figure out how to do that. Do I have to open the file with File.Open, then open another one in Documents and write to that one line by line? That doesn't seem efficient or correct.
Does anyone have any examples of this?
What is the "Copy to Output Directory" set to for you db file? I'd guess the most likely problem is that every time you build in the IDE, the blank db is overwriting the modified db from your last session.