Underonesky
Underonesky

Reputation: 83

Windows Form not getting updates from my database?

Ive only recently starting fiddling with Visual Basic Express and Sql Databases.

Ive managed to get a database up and running, and can query information from it. I have even created a form that can add a new entry to the table im using.

The first form has a ComboBox that list the PlayerNames in my table. Form2 allows you to add a new name to the table, but anything I add isnt immediately updated in Form1. I have to relaunch the program to see the new entries. Even then, these new entries dont seem to be permanent as they eventually dissappear.

The code I have for Form1:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim db = New PlayerTestDataContextDataContext()
    Dim PlayerList = From List In db.Players
                     Select List.PlayerName

    For Each PName In PlayerList

        cbPList.Items.Add(PName)

    Next

End Sub


Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

    frmNewPlayer.Show()

End Sub

End Class

The code for Form2:

   Private Sub btnCPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCPlayer.Click

    Dim db = New PlayerTestDataContextDataContext()

    If txtNewPlayer.Text = "" Then
        lbWarning.Text = "Please enter a name!"
    Else
        Dim Plyr As New Player With {
            .PlayerName = txtNewPlayer.Text}
        db.Players.InsertOnSubmit(Plyr)
        db.SubmitChanges()
        Me.Close()

    End If

End Sub

Not sure what is going wrong here...any help is appreciated. If I have overlooked an obvious answer around here forgive me, Im not sure what I need to be looking for.

Upvotes: 0

Views: 177

Answers (2)

Akram Shahda
Akram Shahda

Reputation: 14781

That should do the trick. But you need to do some reading ...

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click    
    if frmNewPlayer.ShowDailog() == DialogResult.Ok
        Dim db = New PlayerTestDataContextDataContext()
        Dim PlayerList = 
        From List In db.Players                     
        Select List.PlayerName  
        ' 
        cbpList.Items.Clear()
        '
        For Each PName In PlayerList
            cbPList.Items.Add(PName)    
        Next
    end if
End Sub

Upvotes: 1

pickypg
pickypg

Reputation: 22332

You never actually tell Form1 to update its list.

This is what is happening now:

  1. Form1_Load
    • Populates your list of Players.
  2. User presses btnCreate (btnCreate_Click).
    • btnCreate launches frmNewPlayer (which I assume is "Form2").
  3. User presses btnCPlayer (btnCPlayer_Click).
    • btnCPlayer inserts the player in the list.
    • btnCPlayer closes Form2.

At this point, you are done with your flow of operation, but Form1 has never been tipped off that a new Player has been added. Nor is there any code that would make the list reload except when Form1 launches (the Form_Load event/method is only called for the first time it is loaded).

You need (pick one):

  • To launch Form2 as a "Modal" window, which means that it blocks Form1 from doing anything until it's closed. This would allow you to check for an update within Form2 after it's closed.
  • A way for Form2 to notify Form1 that it has added a player ("Event").
  • Setup something to refresh the list (probably using a "Timer").

The modal approach would be the easiest.

Upvotes: 0

Related Questions