Results 1 to 5 of 5

Thread: Problems with database connection

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    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.

  2. #2
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Re: Problems with database connection

    Quote Originally Posted by billboy View Post
    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

  4. #4
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Problems with database connection

    This is most likely happening because you are not repopulating your datasource after the add or remove operation.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width