Problems with database connection
Hello all, some help here would be greatly appreciated
So basically, I'm making a system that connects to a database that contains lecturers and their details, and I've coded into my program a form that will add lecturers and a form that will remove lecturers. My only problem is that in the program, when I've added or removed a lecturer, it will still display in the program, unless I stop and start the program, then the changes will show.
I want the changes to be show instantly, what would I do so that it automatically updates or refreshes in the program?
Thank you.
Re: Problems with database connection
This will depend on how you are connecting to your database and since you havent shown any code we have no idea.
Show us how you are connecting
Re: Problems with database connection
Quote:
Originally Posted by
billboy
This will depend on how you are connecting to your database and since you havent shown any code we have no idea.
Show us how you are connecting
Using the OleDB
Public Class frmRemoveLecturer
Private DBConnection As New OleDb.OleDbConnection
Private DBDataAdapter As OleDb.OleDbDataAdapter
Private DBCommandBuilder As OleDb.OleDbCommandBuilder
Private dtUsers As New DataTable
Private RowPosition As Integer = 0
Private Valid As Boolean
Private ID As String
Private Forename As String
Private Surname As String
Private Sub frmRemoveLecturer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DBConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Users\Kieran\Desktop\A2 Computing Project\Project\MathsQuizSystem.mdb"
DBConnection.Open()
DBDataAdapter = New OleDb.OleDbDataAdapter("Select * from Lecturers", DBConnection)
DBCommandBuilder = New OleDb.OleDbCommandBuilder(DBDataAdapter)
DBDataAdapter.Fill(dtUsers)
Me.ShowDetails()
End Sub
Re: Problems with database connection
This is most likely happening because you are not repopulating your datasource after the add or remove operation.
Re: Problems with database connection
If the list you see is not changing, it's because you're not changing it. Consider this: if I sent you into a room full of people and you took down everyone's name, then I added a few new people and removed a few existing people, would you expect your list of names to magically change? I would hope not, and your app is the same.
Here's what you should be doing:
1. Retrieve the list from the database, i.e. execute a query and populate a DataTable.
2. Edit that list, i.e. add new rows to the DataTable and delelte rows in the DataTable.
3. Save the changes in the list, i.e. update the database from the DataTable.
As you can see, the contents of the list changes before the contents of the database, so your situation could not possibly occur. You are most likely using a data adapter to Fill a DataTable from the database. After modifying the DataTable, you use that same data adapter to Update the database from the DataTable. Follow the CodeBank link in my signature and check out my Retrieving & Saving Data thread for an example of that and several other common ADO.NET scenarios.