This is my first post on stackoverflow so please hang with me. I am attempting to update an access database using VB.NET. Using the following series of videos (starting with this one), we have been able to properly use our form to save new data into the access database.
https://www.youtube.com/results?search_query=vb.net+ms+access+database+tutorial+1+%23+add+new+remove+save+data+in+database+using+vb.net
However, when we start up the form again after opening up access to make sure the updates have occurred (which they always do), none of our changes are there. And when we open up access again the changes we made that showed up in the access database are gone, and the access database goes back to its original state (the state it was when we linked it to visual studio in the first place).
Below is the code we have so far, we appreciate any help you can give us!
Thanks.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Sheet1BindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Sheet1BindingNavigatorSaveItem.Click
Me.Validate()
Me.Sheet1BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(MIS275_Small_BusinessDataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MIS275_Small_BusinessDataSet.Sheet1' table. You can move, or remove it, as needed.
Timer1.Start()
Me.Sheet1TableAdapter.Fill(MIS275_Small_BusinessDataSet.Sheet1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Sheet1BindingSource.AddNew()
End Sub
Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
Try
Sheet1BindingSource.EndEdit()
Sheet1TableAdapter.Update(MIS275_Small_BusinessDataSet.Sheet1)
MessageBox.Show("Data Saved")
Catch ex As Exception
MessageBox.Show("Error")
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim count As Integer
count = Sheet1BindingSource.Count
End Sub
End Class
Related
I am trying to learn SQLLite (Years of Experience with apps using SQL Server). I am going slowly and right off the bat I could not open a connection to a database created in SQLite Studio. I have tried a number of different examples with no luck. I first tried with a Nuget package and then went to the SQLite website and downloaded the 64-bit binaries for .Net 4.5. I get this error no matter what I try: system.BadImageFormatException.
Can someone please tell me what I'm doing wrong or point me to an example that really works?
Thanks!!!
Imports System.Data.SQLite
Public Class Form1
Private connectionstring As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim exePath As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
Dim dbPath As String = exePath & "\NamesTest.db"
connectionstring = "Data Source=" & dbPath
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SQLiteConn As New SQLiteConnection(connectionstring)
SQLiteConn.Open()
End Sub
End Class
I've created the table: "tblInterni" on my sql database and made so that I can see it on a datagridview.
I am now making a search function so that if I search for a name it loads everyone with that name in the datagridview, but the query I made isn't working.
Private Sub Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim conn = New SqlConnection("Data Source=SRV-SQL;Initial Catalog=dbTest;User ID=pwdDb;Password=pwdDb")
Dim adapter As New SqlDataAdapter("SELECT * FROM tblInterni", conn)
Dim table As New DataTable()
adapter.Fill(table)
DataGridView1.DataSource = table
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim searchQuery As String = "SELECT * From tblInterni WHERE name like '%" & TextBox1.Text & "%'"
End Sub
form graphic
Given that you are retrieving all the data when the form loads, what you should be doing is binding your DataTable to the DataGridView via a BindingSource and then filtering that data by setting the Filter property of the BindingSource.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using adapter As New SqlDataAdapter("SELECT * FROM MyTable", "connection string here")
Dim table As New DataTable
adapter.Fill(table)
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Using
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
BindingSource1.Filter = $"MyColumn LIKE '%{TextBox1.Text}%'"
End Sub
Note that the BindingSource would be added in the designer, just like the grid.
This is still not ideal though. If the user wants to type several characters in order to filter then this code will modify the filter several times unnecessarily and actually slow them down. A better idea is to use a Timer to add a small delay before filtering that resets each time they make a change. That way, if they type several characters quickly enough, the filter will only change after the last character.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using adapter As New SqlDataAdapter("SELECT * FROM MyTable", "connection string here")
Dim table As New DataTable
adapter.Fill(table)
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Using
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Start/reset the filter timer.
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
BindingSource1.Filter = $"MyColumn LIKE '%{TextBox1.Text}%'"
End Sub
You can experiment a bit with the Interval of the Timer but you should find that something around 300 milliseconds should mean that filtering still feels fast enough but typing at a reasonable speed should avoid most unnecessary intermediate filters.
I have created a database file in MS access and linked to vb.net form. When i delete a record at run time it shows deleted in user interface but when i checked the Database file its stills in DB file what should i do ?
I have used this function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
StudentData1BindingSource.RemoveCurrent()
End Sub
Try saving after you remove the record. Should look something like this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
StudentData1BindingSource.RemoveCurrent()
Me.Validate()
Me.StudentData1BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.StudentData1DataSet)
End Sub
I have attached code for reference. I am trying to create database for my project details. I have currently 2 forms:
main form
project detail form
Main form used to switch various forms
Project detail forms are meant for add / edit / load project details
My main form in visual basic look like this.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Bt_Project_Details_Click(sender As Object, e As EventArgs) Handles Bt_Project_Details.Click
Me.Hide()
Project_Details_Form.Show()
End Sub
End Class
My project detail form look like this.
Public Class Project_Details_Form
Private Sub Project_Details_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Project_Data_Set.Project_Data_Table' table. You can move, or remove it, as needed.
Me.Project_Data_TableTableAdapter.Fill(Me.Project_Data_Set.Project_Data_Table)
End Sub
Private Sub Bt_Load_Project_Click(sender As Object, e As EventArgs) Handles Bt_Load_Project.Click
End Sub
Private Sub Bt_Cancel_Click(sender As Object, e As EventArgs) Handles Bt_Cancel.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Project_Data_TableBindingSource.AddNew()
End Sub
Private Sub Project_Data_TableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Project_Data_TableBindingNavigatorSaveItem.Click
Me.Validate()
Me.Project_Data_TableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Project_Data_Set)
End Sub
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
Project_Data_TableBindingSource.RemoveCurrent()
End Sub
Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs)
Try
Me.Project_Data_TableTableAdapter.FillBy(Me.Project_Data_Set.Project_Data_Table)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Now I am facing a few problems:
Whenever I run the program, try to enter first entry data being saved but with empty value. The data enter next time being stored properly. How can make my first entry visible?
In below code:
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Project_Data_TableBindingSource.AddNew()
End Sub
I am using AddNew(). This working fine, but problem is even I don't enter data like date, name kept empty, data recorded in their position is store empty only. I would like to say if any of project data set is empty give error message (since it mandatory for user to enter those data)
Is there any other method where i can assign value individually .
Like my Project database contain project name, customer name etc. if i say new button pressed clear all field. Once form filled individual value being record once save button pressed
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Project_Data_TableBindingSource.AddNew()
End Sub
Am still learning VB and database but I have a little problem. My codes works but when I
try to look into the database I don't find all what are been saved, how do I rectify it? and also the edit and delete. thanks
Here is my code:
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 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
End Sub
Private Sub ItemsDataSetBindingSource_CurrentChanged(sender As Object, e As EventArgs)
End Sub
Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click
ItemsBindingSource.EndEdit()
ItemsTableAdapter.Update(ItemsDataSet.Items)
MsgBox("Saved")
End Sub
Private Sub btadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
ItemsBindingSource.AddNew()
MsgBox("Added Successfully")
End Sub
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
ItemsBindingSource.RemoveCurrent()
MsgBox("Item Deleted")
End Sub
Private Sub btnexit_Click(sender As Object, e As EventArgs) Handles btnexit.Click
Close()
End Sub
Private Sub btnprevious_Click(sender As Object, e As EventArgs) Handles btnprevious.Click
ItemsBindingSource.MovePrevious()
End Sub
Private Sub btnnext_Click(sender As Object, e As EventArgs) Handles btnnext.Click
ItemsBindingSource.MoveNext()
End Sub
End Class
You probably have your MDB (ACCDB) file included in your project files.
If you check the properties associated with this project file you will find one named Copy to Output Directory. Set this to Copy Always.
Also your connection string contains the shortcut Data Source = "|DataDirectory|\yourdb.mdb"
If this scenario is correct, then you start your application and the MDB / ACCDB file is copied from the original location to the BIN\DEBUG directory.
You insert your data there without error. When you stop the debug session to fix errors or other problems you restart your application and the fresh copy (but empty) of your db is copied again in the output directory.
To fix this, set the property to Copy Never or change your connection string to point to a fixed location.