Reputation: 83
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
Reputation: 14781
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
Reputation: 22332
You never actually tell Form1 to update its list.
This is what is happening now:
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):
The modal approach would be the easiest.
Upvotes: 0