none
DataSet and local (embedded) database RRS feed

  • Question

  • I have been playing with DataSet and small, local, embedded databases (mdf-files). At first it all looks great, the data that i insert into the database using VS Server Explorer i shown correct in the GUI. Adding new data through the GUI appears to go well too. However, when I restart the application the data just added through the GUI is not to be found - only the initial data entered in VS Server Explorer is shown.

     

    My guess is that data is never persisted in the mdf-file, but I have been unable to verify this and find a solution to the problem.

     

    Code snippet showing how I insert data runtime:

     

    Code Snippet

    BookHereDataSet.HistorikRow row = bookHereDataSet.Historik.NewHistorikRow();

    row.Kunde = (Guid)Kunde.SelectedValue;
    row.Medarbejder = (Guid)Medarbejder.SelectedValue;
    row.Noter = Noter.Text;
    row.Dato = Dato.Value;
    row.Guid = Guid.NewGuid();

    bookHereDataSet.Historik.AddHistorikRow(row);

     

     

     

    Monday, April 14, 2008 9:43 AM

Answers

  • Finally it seems to be working. I had the update-functions as mentioned by you, so that wasn't the problem. Instead, the problem was that I was using "bookHereDataSet.Historik.AddHistorikRow(row);" instead of "bookHereDataSet.Historik.Rows.Add(row);".

     

    I have no plausible reason of why the AddHistorikRow acts different from Add, but it works :-)

     

    Tuesday, April 15, 2008 5:54 AM

All replies

  • Hi,

     

    I guess the local database is in your project directory.

    In the Solution Explorer select the database at choose properties from the context menu. There is a property called 'Copy toOutputDirectory' which is set to 'Copy always' by default. Change this to 'Do not copy' and your application should work.

     

    During the build process Visual Studio is copying the database from your project directory to the output directory (<project directory>/bin/Debug or .../bin/Release).

    If you made changes through your application these changes are done in the local database in the output directory.

    If you use the server explorer the changes are done to the database in the project directory.

     

    regards

    Philipp

    Monday, April 14, 2008 11:18 AM
  •  

    Thanks for the quick reply - however it didn't help much. I tried setting the property you mention, but nothing new happens. New data is still not persisted in the database :-(

     

    Monday, April 14, 2008 11:42 AM
  • How do you check if the data is in the database?

    Do you use the server explorer? It will only show the data from the database in your project directory.

    Does the application shows the new data after starting it a second time?

     

    regards

    Philipp

    Monday, April 14, 2008 11:59 AM
  • Here is what I do:

     

    1) Create initial database data using Server Explorer

    2) Start my application

    3) Add data via my application

    4) Visually assert that data is added

    5) Restart my application

    6) Data added in step 3 is not shown

    Monday, April 14, 2008 12:52 PM
  • Ok.

    The code you posted inserts a new row to the DataTable Historik in your DataSet bookHereDataSet.

    What's the code you use to save the changes to the database?

    Monday, April 14, 2008 1:04 PM
  • I'm a newbie to C# and .NET - so I might be missing some bits :-) I was under the impression that generating a dataset/datatableadapter/etc using the Toolbox in the GUI-builder was sufficient.

     

    What do I need to adjust to persist the changes from the dataset to the physical database-file?

     

    Thanks

    Monday, April 14, 2008 5:27 PM
  • The DataSet works as a offline cache for database data.

    You have to load from database

    historikTableAdapter.Fill(bookHereDataSet.Historik);

    and you have to save to database.

    In VS2008 the code would look like:

    Validate();    // the last control before clicking the save button is validated

    historikBindingSource.EndEdit();    // the editing of the last record is treated as finished

    // the above two statements are only necessary if you bind your DataSet to UI controls

    tableAdapterManager.UpdateAll(bookHereDataSet);   // this triggers the database updates

    In VS2005 the last statement should be replaced by

    historikTableAdapter.Update(bookHereDataSet.Historik);

    You can call these Update calls multiple times without reloading the data.

     

    If you drop from the Data Sources Window in VS to your form this code should be generated by VS. In this case it's the click handler for the save button in the navigator tool bar.

     

    Is there such code in your code file?

    If yes can you post the code used for saving.

     

    regards

    Philipp

    Monday, April 14, 2008 6:14 PM
  • The Fill-code is already there, but the validate/endedit/updateall is not present. I will look into that - thanks!

     

    Monday, April 14, 2008 6:18 PM
  • Finally it seems to be working. I had the update-functions as mentioned by you, so that wasn't the problem. Instead, the problem was that I was using "bookHereDataSet.Historik.AddHistorikRow(row);" instead of "bookHereDataSet.Historik.Rows.Add(row);".

     

    I have no plausible reason of why the AddHistorikRow acts different from Add, but it works :-)

     

    Tuesday, April 15, 2008 5:54 AM
  • This shouldn't be the case.

    Visual Studio generates classes for your typed dataset. They can be viewed if you go the solution explorer and click on the icon 'Show All Files' in the toolbar of the solution explorer. There should be a file named BookHereDataSet.Designer.cs. This file contains a class BookHereDataSet.HistorikDataTable and this class defines a method AddHistorikRow(BookHereDataSet.HistorikRow row). This is the one called first by your code and this method executes one statement this.Rows.Add(row) which is nothing else then bookHereDataSet.Historik.Rows.Add(row); which you called second in your code.

    If this isn't the case something is wrong with the generated typed dataset.

    Tuesday, April 15, 2008 10:35 AM