Adding data in vb.net application does not change ms access database file - vb.net

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.

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:
Copy always - always overwrites red db with blue db
Copy if newer - copy means db have same date, then your program edits red DB so it is newer, build process doesn't overwrite it.. If you make a change to the blue DB (add a table etc) then blue DB is newer, build will overwrite red db. This is a good setting to use
Copy never - you must manually copy the db
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

Related

How to delete a record in VB.net with Access database?

I am a beginner so I followed a tutorial on YouTube and made a program in VB.net using Windows Form that connects to an Access database. The program works, it can move to Previous record, Next record, Add new record and Save. However, the Delete button doesn't work.
I have tried searching for solutions and it was mentioned that I needed to have a Primary Key in the Access database or it wouldn't create a Delete command. So, I set a Primary Key then ran the program but Delete still doesn't work.
I've read that I need to configure the TableAdapter Configuration Wizard but they didn't specify on what exactly I should do. I tried messing around a bit and when I clicked on the Properties of the Table Adapter, I now see a DeleteCommand. However, I'm not sure of what to do with it or how to make it work.
This is what the delete button does
Private Sub DeleteBtn_Click(sender As Object, e As EventArgs) Handles DeleteBtn.Click
PatientRecordsBindingSource.RemoveCurrent()
End Sub
And this is what the save button does:
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
On Error GoTo SaveErr
PatientRecordsBindingSource.EndEdit()
PatientRecordsTableAdapter.Update(DatabaseDataSet.PatientRecords)
MessageBox.Show("Ok")
SaveErr:
Exit Sub
So, I run the program and I can add and save new records in the table. But when I select a record from the table and then click the Delete button, it only deletes the record on the program. When I check the Access file, the supposedly deleted record is still there. But then I ran the program again and the record I tried to delete is back in the table, like nothing happened.
EDIT: I created a new Access Database file, made sure I assigned a Primary Key then tried it on a new form. It worked, but I am still wondering if I can still fix my problem with the old form.

Setting the value of a variable in Visual BASIC

Hi i'm new in programming world and I've started my programming by Visual BASIC. I'm trying to set a value of a variable through the program closing event and load the same value in program load event. As example:
At first I tried:
Dim Age as Integer
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Age = 50
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Textbox1.text = Age
End Sub
But when I close the program and restart it, it resets to zero.
Next I tried "Settings" from properties but if I move my program from one location to another then it also resets everything.
Finally I tried Stream readers and writers to catch the final value but for this I had to attach some text files to the programs which I don't want. Can anyone help me how to solve the following problem with custom class libraries or by something else?
All the variables that you have are run time variables. What that means is it will only have value till your program is running.
If you want to store the data taken from user, you store it in database. You can use any kind of database and connect it to your program, store everything in there. You can also retrieve the values from database to use in system when you restart your program.
If you are new to programming and trying to learn visual basic, start with basic concepts or pick up a book that start with basics. Once you know the basics of programming, you can read about connecting program to database.
The value of age is never going to remain static when the application is shut down unless you keep it in the application settings or some sort of flat file, database etc. The initial value could be set in the application when the form opens or from at static value in application settings, but unless you store the value somewhere other than memory, it will not persist.
What you can do is create a form called module1.vb . in this form declare and set your variables . . .
Example
Public Age As Integer = 50

VB .net Clickonce - check for update but dont update

ok so ive got an updating program via clickonce, I want it to notify the user there is an update but don't actually update the program until an admin logs on and requests the update to go ahead.
I'm checking for updates like this via code
If My.Application.IsNetworkDeployed() Then
If My.Application.Deployment.CheckForUpdate() Then
MsgBox("Updates are available", vbInformation, "Updates available")
Note I haven't called the
My.Application.Deployment.Update()
to actually update.
When my application checks for updates it displays ok, but when no one does anything else when it is shut down again and then started up - it seems to revert back to automatically downloading the update on program startup. I have update automatically turned off in the project properties
I tried not checking for updates and the program starts and doesn't update so I'm thinking that just the act of checking and finding an update automatically sets the program to download it next time its started. which id rather it didn't
has anyone come across this issue before?
thanks
Modify this code. It is with an "updating" action but you'll be able to change this
Imports System.Deployment.Application
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim updateCheck = ApplicationDeployment.CurrentDeployment
Dim info = updateCheck.CheckForDetailedUpdate()
If (info.UpdateAvailable) Then
MsgBox("Update wird geladen.")
updateCheck.Update()
MessageBox.Show("The application has been upgraded, and will now restart.")
Application.Restart()
End If
Catch : End Try
Form1.Show()
Me.Close()
End Sub
Do you by any chance have these options selected??
If you're handling the updates programmatically, you need to untick the "The application should check for updates" box.
Doing this will grey-out the "After" and "Before" radio buttons below it.

How to save a changed label when you exit

In my program I have a preview, and edit side.
When you edit using the text boxes on the edit side(right side) and click "save", It should change the label on the right side (preview side). Although when you exit the program and re-open, all the data you entered has disappeared!,
I have tried the below code and had no luck as my result.
Public Class Form1
Private Shared NameBasic As Integer
Public Sub New()
InitializeComponent()
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NameBasic = txtFirstBasic.Text
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
End Class
Also my goal is to have it be able to take it on a flashdrive onto any computer and have that data still saved within the exe. Is this even manageable? (I am more of a web based programmer so I am a bit new to this)
You need to write values to a text file at the same location of the exe. and read it. you will need both exe and the textfile.
OR
You will need to write a dll on run-time and write some values while closing the application. this will be more secure then the text file. here you will need both exe and dll to be copied into flash drive
OR
the application setting as spoken by others..

VB 2013 Persistent User Settings

On my form I have a menu with "file-save". When I click save I want to save particular settings to restore when the form is closed and re-opened. I've done this successfully for text in text-boxes and the checked states of check-boxes, but I'm failing when trying to loop through the items in a list-box. Please see below for what I've tried...
When I click save:
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs)
Handles SaveToolStripMenuItem.Click
For Each i In ListBox1.Items()
My.Settings.ListBox1.Add(i)
Next
My.Settings.Save()
End Sub
When my form loads:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
For Each i In My.Settings.ListBox1()
ListBox1.Items.Add(i)
Next
End Sub
I've only been using VB for three days, so apologies if I am missing something simple ha! Thanks for any help!!!
There is one small glitch with the StringCollection in settings. If you do not seed it with a fake variable then it starts out as Nothing and you cannot add to Nothing. in your form load add this:
' if the collection has not been initialized, do so
If My.Settings.ListBox1 Is Nothing Then
My.Settings.ListBox1= New System.Collections.Specialized.StringCollection
End If
' now it is safe to use: load strings from Setting -> form listbox
For Each s As String In My.Settings.ListBox1()
ListBox1.Items.Add(s)
Next
The very first time it runs, there are likely no saved settings, so we have to create the container for them, basically.
Option Strict can be implemented by file, by adding this at the top:
Option Strict On
Or for the project: Project => Properties => Compile: Option Strict is likely to the right (I have 2012). You can also set it as a permanent option (recommended).
Among other things, this will prevent you from plucking variables out of the air and use them without declaring a type (which will lead to errors). For instance:
For Each i In My.Settings.ListBox1()
becomes
For Each s As String In My.Settings.ListBox1() ' tell the compiler the Type