Reputation: 47
I am creating a library software. I have connected an access database with my vb application. In "Add book form", whenever i add a book in there, it shows in datagridview below it but as i restart the application, i find that data is gone as saving data through form is not affecting my database file. Here is the code:
Public Class addForm
Private Sub BooksBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.BooksBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataDataSet)
End Sub
Private Sub BooksBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles BooksBindingNavigatorSaveItem.Click
Me.Validate()
Me.BooksBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataDataSet)
End Sub
Private Sub addForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DataDataSet.Books' table. You can move, or remove it, as needed.
Me.BooksTableAdapter.Fill(Me.DataDataSet.Books)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
BooksBindingSource.EndEdit()
BooksTableAdapter.Update(Me.DataDataSet.Books)
MessageBox.Show("Book added successfully")
Catch ex As Exception
MessageBox.Show("Error")
End Try
End Sub
End Class
Name of my table is "Books". And also it shows "Book added successfully" after clicking on add btn.
Upvotes: 0
Views: 53
Reputation: 74660
Here is a screenshot of the folder structure of one of my projects that uses an access db:
Circled in BLUE is the accessdb that I added to the project. This is the DB file I see in Solution Explorer
Circled in RED is a copy of the blue DB; it is copied to the bin\Debug folder every time I run a build (start the app)
The program runs from the bin\Debug folder (or release if you're using that build configuration) and it saves its data in the RED database
This means every time my program saves a change to the DB, the build process will overwrite the red DB with a fresh copy of the blue DB, next time the build process runs
So, every time I start the app, previous modified database is overwritten with a blank one
I can change this behavior if I click on the blue DB in solution explorer:
VS probably gave some info about this when you first added the DB, but the dialog it shows up is wordy and boring, so most people don't read it or don't really get what it's talking about even if they do read it:
Clicking Yes eventually causes you to ask a question like this on SO :) - no bad thing, but I do wish MS had made "Copy if Newer" the default..
Other notes about your code:
The topmost Sub BooksBindingNavigatorSaveItem_Click
) is redundant - remove it
The btnAdd_Click
method is nearly an identical re-statement of BooksBindingNavigatorSaveItem_Click_1
and is hence probably redundant - it should be removed. If you have two buttons intended to save data, put the save code into a sub and call it from both button click handlers; don't repeat yourself
Upvotes: 1