-1

I am using a server based database in vb.net. Why is a copy of the databasefile (.mdf) also created in the bin folder when I run my application. I think this is the reason why my datagridview does not update my sql table with the following code :

Private Sub SAVE_Click(sender As Object, e As EventArgs) Handles SAVE.Click

    Validate()
    ParsreelsBindingSource.EndEdit()
    TableAdapterManager.UpdateAll(SkeduleringDatabasisDataSet)
    Close()

End Sub
djv
  • 15,168
  • 7
  • 48
  • 72
Gideon
  • 313
  • 1
  • 2
  • 18
  • 1
    Take a look at [Why saving changes to a database fails?](https://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails) – Ňɏssa Pøngjǣrdenlarp Nov 13 '17 at 17:50
  • What is your connection string? – Joel Coehoorn Nov 13 '17 at 19:03
  • Thank you Plutonix. That did not work for me. I changed the .mdf file (in solution explorer) and set the property Copy to Output directory to : Do not copy. I ended up with the following error :An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: An attempt to attach an auto-named database for file C:\Users\Administrator\Desktop\Skedulering\Skedulering\Skedulering\bin\Debug\SkeduleringDatabasis.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. – Gideon Nov 13 '17 at 19:40
  • Hi Joel my connection string is : Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Administrator\Desktop\Skedulering\Skedulering\Skedulering\SkeduleringDatabasis.mdf;Integrated Security=True – Gideon Nov 13 '17 at 21:22
  • You should have changed it to "Copy if newer". If you don't copy at all then of course there's no database to connect to. Everything was working correctly to begin with, i.e. your changes were being saved. You just weren't looking for them properly. The changes are made to the copy to keep your source database clean, so it's pristine when it comes time to build a Release version of your app and not clogged up with test data. – jmcilhinney Nov 13 '17 at 22:35

1 Answers1

1

You add a data file to your project and that is a source file, just like any other. You don't connect to that file at run time because you don't want to fill it with test data and potentially delete some default data. That data file should remain clean.

When you build, the clean source file is copied to the output folder along with your EXE by default. It's that copy that you connect to at run time. You can make as much of a mess of it while testing as you like, safe in the knowledge that your source file is unaffected. If it's a Release build, you have a clean database to deploy with your app.

By default, the Copy to output directory property of the data file is set to Always copy. That means that every time your project is built, a new copy of the data file is made in the output folder. If you run your project twice without making any code changes, there is no build so there is no new copy, so any changes made during the first run will remain. If you make a code change though, the project is built before the second run so your data file is overwritten with a new copy, so your changes will be lost. It's that behaviour that confuses people who either don't read about how to manage local data files or don't read it properly. Your changes ARE saved. They are simply overwritten next time you build.

If you set Copy to output directory to Copy never then no database is copied to the output folder. If your code then tries to connect to a database in that location it will obviously fail. You would need to copy the data file yourself to that location or, if you change the connection string, to another location.

The most sensible option is to set Copy to output directory to Copy if newer. In that case, a new copy of the database will not be made unless you've made a change to the schema or default data in the source data file. That means that changes made while debugging will survive between sessions. You may find that a new copy is made sometimes when you don't want it to but that's still better than the alternative, which is making a new copy every time. If you don't touch the source data file, which you have no reason to do if you're not changing the schema or the data, then your test data will survive across sessions.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Thank you. Thank you jmcilhinney. That was extremely well explained. I understand now. I have just modified the connectionstring to the .mdf file in the BIN/DEBUG folder as I need to change data in the source file to test my application. I will change the connectionstring back once I am ready to build a Release version. Once again may I express my gratitude. – Gideon Nov 14 '17 at 16:50
  • You DO NOT need to change data in the source file to test your application. Thousands of developers the world over do things the way they were intended. I'm not sure why you think that your situation is special but it's not. What you do is up to you but there's no good reason that I can think of for you to do it the wrong way. – jmcilhinney Nov 14 '17 at 21:43
  • When i update with the database table with a DGV as in the above code the changes are not reflected in the original table only in the file in the BIN/DEBUG file. Checked it with a mdf viewer. – Gideon Nov 15 '17 at 19:18
  • @Gideon, you are correct, and that's exactly the way it's supposed to be. That way, you can make a mess of that copy in the 'bin' folder as much as you want while testing/debugging and still have a pristine source file ready to be deployed to your end users. Why exactly do you think that you particularly need to change your source file? What does that do for you that all those other thousands of developers the world over don't need? – jmcilhinney Nov 15 '17 at 22:31
  • I was hoping to see the changes directly in my source file via the server explorer while testing my software. – Gideon Nov 17 '17 at 13:17
  • Any reason you can't view the working copy via the Server Explorer? I've never tried but I would expect that you should be able to. – jmcilhinney Nov 17 '17 at 13:39