How do i update different table from the same access database using visual basic .net - vb.net

I added my Enrollment system access Database, into my Enrollment System vb.net form, as a data source. The Database has 2 tables in it, the accountTable and studentEnrollmentInformation. I dragged The accountTable's details and data grid view into my form designer. The following code automatically appeared in the code designer:
Private Sub AccountTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
End Sub
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Sub
The following code works for updating the accounTableDataGridView but it does not work for studentEnrollmentInformationDataGridView so i manually created one
for studentEnrollmentInformation.
Function updateStudent()
Me.Validate()
Me.StudentEnrollmentInformationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
Me.StudentEnrollmentInformationTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.studentEnrollmentInformation)
End Function
This is the function that contains the update code, that i manually created for updating the studentEnrollmentDataGridView. Adding new Row works fine but when i try to update studentEnrollmentDataGridView the texts in the table disappears and does not update/save. I also had function for updating the accountTableDataGridView which works fine.
Function update() 'THIS FUNCTION CONTAINS PRE-MADE CODE TO MAKE UPDATING SHORTER IN WRITING CODE.
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Function
My Question is how do i update multiple Tables in my system? Updating the other table works fine but the other is not.

In the original auto-generated code, this is the line that retrieves the data in the first place:
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Sub
When the form loads, the Account data is retrieved into a DataTable that is already bound. If you want to retrieve Student Enrollment data too, do it in the same place:
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
Me.StudentEnrollmentInformationTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.studentEnrollmentInformation)
End Sub
Now you're populating both bound DataTables when the form loads. When it comes to saving, you do the same thing, i.e. add the code to save the changes to the other DataTable where you already have the code to save the first:
Private Sub AccountTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.StudentEnrollmentInformationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
End Sub
You don't need any code to specifically save the changes from the DataTable because the whole point of UpdateAll is that it updates all DataTables in the DataSet.
As is always the case, if it doesn't seem to be working as you expect then you debug it. In that case, that would mean setting a breakpoint on the UpdateAll line and examining the exact state of the DataSet before and after the call, as well as possibly examing the sate of the database too.

Related

VB.Net 2013 How do I programatically update a bindingsource without getting late binding errors/warnings?

I created a DataSet with a DataTable & TableAdapter that contains Select/Update/Insert/Delete commands that relate to my MSSQL database. The DataTable selects all the fields from a Person table (two fields in particular a LastName string and an UpdateUserID int).
Then I made a simple test form with a textbox and a button. From the Data Sources tab, I dragged the LastName field onto the form. This creates MyDataset, MyTableAdapter, and MyBindingSource on the form. It also databinds the textbox to the LastName field of the BindingSource.
Here's the complete code for the form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
loadData()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Validate()
Me.MyBindingSource.EndEdit()
' Me.MyBindingSource.Current("updateUserID") = 123
Me.MyTableAdapter.Update(Me.MyDataset.myDatatable)
loadData()
End Sub
Private Sub loadData()
Me.MyTableAdapter.Fill(Me.MyDataset.myDatatable)
End Sub
End Class
This all works completely fine. The problem occurs when I uncomment the line: "Me.MyBindingSource.Current("updateID") = 123". I'm trying to set the UpdateID to save the ID of the user that most recently made a change.
In Visual Studio 2008, this worked fine. But I just upgraded to 2013 and now I'm getting an error that says "Option Strict On disallows late binding."
(PS, this is a test program. In the actual program - converted from 2008 - I only get warnings saying "Late bound resolution; runtime errors could occur.")
I understand why I'm getting these errors/warnings: because the IDE doesn't know what DataType the "updateUserID" DataColumn is.
But what can I do to manually update the BindingSource before saving? I don't think turning Option Strict off is a good idea.
The DataColumns in the DataTable have their DataTypes specified. Is there a way to reference the DataColumn directly instead of through the string of its name?
I'm looking for something like:
Me.MyBindingSource.Current.DataColumns.UpdateUserID.value = 123
Thanks for any help!

Update changes made in datagridview into Access 2016

I made a connection with an Access database (Access 2016) using dataset, bindingsource, tableadapter, bindingnavigator, and datagridview.
It works, I can navigate in the datagridview, make changes, add and delete records in the datagridview, but these changes don't appear in the Access DB.
Data is loaded with:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'FacturatieDataSet.Catalogus' table. You can move, or remove it, as needed.
CatalogusTableAdapter.Fill(FacturatieDataSet.Catalogus
End Sub
For deleting I use:
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
CatalogusBindingSource.RemoveCurrent()
CatalogusTableAdapter.Update(FacturatieDataSet.Catalogus)
End Sub
I'm new with VB 2015, I'm not a programmer, I do this for personnal study.
What is an (easy) solution to my problem?
You fill the datagridview but you don't update it (except when you delete a record).
Look up a tutorial on how to handle basic CRUD operations with a datagridview.

VB.NET/Access DataGridView not displaying table contents

I'm attempting to display an Access Database table in a DataGridView on the most simple form you could make. However, when I start the application I don't see any of the table data. See below.
I'm happy to update with any additional information needed.
VB:
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MOKANDataSet.SalaryHistory' table. You can move, or remove it, as needed.
Me.SalaryHistoryTableAdapter.Fill(Me.MOKANDataSet.SalaryHistory)
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Here is the application at runtime.
Overview of project in Visual Studio
I was curious and went directly to the executable in the bin folder. Upon execution, I got "The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine. I'm using Win 7 64 Bit, but the Microsoft Office suite I have is 32-bit. I got the 32-bit AccessDatabase Engine referenced here

When to create a procedure(sub, function)

I'm currently creating my application and I'd like to write an understandable code.
I'd like to know when to create a procedure. If it affects the performance of my application if I created much.
Here is my sample:
Sample Code:
With Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Greetings()
End Sub
Private sub Greetings()
MessageBox.Show("Hello!")
MessageBox.Show("To")
MessageBox.Show("My")
MessageBox.Show("World")
End
In the example above, assume that this sub will only be called 1 - 2 times in the whole application. I like to easily understand my code.
Versus
Without Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Hello!")
MessageBox.Show("To")
MessageBox.Show("My")
MessageBox.Show("World")
End Sub
In the example above, this piece of code too my time to understand.
In general, you should create subroutines when the logic is going to be used multiple times, especially if it's being used in multiple places.
If you ever have to change code, you want to only change it ONCE.
Performance with running basic code or calling functions is a non-issue. Do not even consider it.

Simple, but I'm stuck ....Updating a DataSet using code

I have a simple Windows form in VB: textbox bound thru an adapter and a bindingsource to my dataset.
I have a button that on Click I want it to update the database. The form loads and the first data row shows in the textbox, I change the text then click my button but no update happens.
Any ideas what I'm doing wrong, or how I should do this??
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AToolsTableAdapter.Fill(Me.Qedsandb_TroyDataSet.aTools)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AToolsTableAdapter.Update(Qedsandb_TroyDataSet.aTools)
End Sub
End Class
Assuming the click event runs(?), TableAdapters based on a query (a join) do not, by default, have the ability to update the database. The name of your binding source suggests that you are using a query.
MSDN: TableAdapter Overview
The update functionality of a TableAdapter is dependent on how much
information is available based on the main query provided in the
TableAdapter Wizard. For example, TableAdapters that are configured to
fetch values from multiple tables (JOINs), scalar values, views, or
the results of aggregate functions are not initially created with the
ability to send updates back to the underlying database. However, you
can configure the INSERT, UPDATE and DELETE commands manually in the
Properties window.
You don't appear to be moving the data back from the form to the dataset. Try calling EndEdit on your bindingsource.