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

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!

Related

Visual Studio 'null' is not defined

I am trying to learn Visual Studio with VB and started by using the Microsoft training by creating a Picture Viewer form.
Everything was going fine and I figured out how to use VB code which is slightly different than the examples in C#. I have most of the training done and can bring in a pictures from the app, but I am having trouble with the closeButton based on the code provided. I am at a loss after searching for many hours. The following is the code from the Open Picture and Close Code. As I was inputting the code from the interface there was no null so I typed it in and that didn't work.
Any help would be greatly appreciated. Thanks
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles showButton.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
End If
PictureBox1.Load(OpenFileDialog1.FileName)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles clearButton.Click
PictureBox1.Image = null
End Sub
Think if you change null to Nothing, you should be all good
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles clearButton.Click
PictureBox1.Image = Nothing
End Sub
Change the keyword null to nothing please .
For non-nullable value types, Nothing in Visual Basic differs from null in C#. In Visual Basic, if you set a variable of a non-nullable value type to Nothing, the variable is set to the default value for its declared type. If a variable is of a value type, the behavior of Nothing depends on whether the variable is of a nullable data type. If a variable is of a reference type, assigning Nothing to the variable sets it to a null reference of the variable's type.
See this document for more details and demo.

How do i update different table from the same access database using visual basic .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.

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 2013: how to update database using datagridview values

I have a DGV in my form that can be edited. I want to write back the updated values to the database so that they are available for next read.
I searched for answers around net and found couple of ways of doing it by opening a connection and updating it. However, i was wondering if there is any direct way of updating the database by using a single command.
I am a novice at VB and learning it only for a library project, hence find it difficult to understand and implement statements that deal with SQLs in the code. pls help... thanks
The proper approach to this is to use a data adapter to populate a DataTable that you then bind to the grid, then use the same data adapter to save the changes from that table back to the database. E.g.
Private adapter As New SqlDataAdapter("SELECT * FROM MyTable", "connection string here")
Private builder As New SqlCommandBuilder(adapter)
Private table As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Query the database and populate the DataTable.
adapter.Fill(table)
'Bind the data to the UI.
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
BindingSource1.EndEdit()
'Save the changes back to the database.
adapter.Update(table)
End Sub
The BindingSource would be added to the form in the designer.

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