r/visualbasic • u/Jaco2point0 • May 08 '18
VB.NET Help [VB.NET]Help with pushing changes back to a database
Alright so I'm working on a final project. I use
Me.LEVEL_INSTANCETableAdapter.Fill(Me.LevelsDataSet.LEVEL_INSTANCE)
Me.L_COLUMNSTableAdapter.Fill(Me.LevelsDataSet.L_COLUMNS)
Me.HIGH_SCORESTableAdapter.Fill(Me.LevelsDataSet.HIGH_SCORES)On form load to grab info from the database. I make changes with queries and stored procedures.
LEVEL_INSTANCETableAdapter.UnlockLevel(intLevelNum)
HIGH_SCORESTableAdapter.UpdateHighScore(SCORE:=intScore, LvlID:=intLevelNum, PlayerName:=strName)These execute just fine, I can see the changes they make elsewhere in my program. then the issue comes when I try to save back to the database. I use.
Me.Validate()
Me.LEVEL_INSTANCEBindingSource.EndEdit()
Me.HIGH_SCORESBindingSource.EndEdit()Then use
Me.TableAdapterManager.UpdateAll(Me.LevelsDataSet)
Me.LEVEL_INSTANCETableAdapter.Update(Me.LevelsDataSet.LEVEL_INSTANCE)
Me.HIGH_SCORESTableAdapter.Update(Me.LevelsDataSet.HIGH_SCORES)The data never goes back to the database (and if I understand the update method correctly, it returns a number that represents the number of rows changed, which for me, are always zero)
I have the dataset, tableadaptermanager, tableadapters, and binding navigators. my database and dataset's "copy to output dir" is set to "copy if newer"
I have also tried another method that starts with this
Dim NewHighScores As LevelsDataSet.HIGH_SCORESDataTable = CType(Me.LevelsDataSet.HIGH_SCORES.GetChanges(Data.DataRowState.Added), LevelsDataSet.HIGH_SCORESDataTable)
Dim ModifiedHighScores As LevelsDataSet.HIGH_SCORESDataTable = CType(Me.LevelsDataSet.HIGH_SCORES.GetChanges(Data.DataRowState.Modified), LevelsDataSet.HIGH_SCORESDataTable)But it seems like getchanges in that line is showing no changes...
I've seen methods which directly interact with the database, ones that directly update, or calling on the database to run queries directly. I'd like to avoid those if possible. I'm really at my last legs here guys, I'll probably pass the class/assignment even if I don't get this working, but I'd like to understand it for the future... Thanks for all your responses in advance.
Edit: formatting
2
u/JamesWjRose May 09 '18
Not the answer you are looking for, but....
Consider using stored procedures to send the data you want back to the database. I have been a db developer for 25+ years and I don't use Data Adapters, so my pov and extended history could be the "wrong" pattern.
I have long used stored procedures to insure that:
There are distinct layers in the application
This allows a level of security for the application as access to the abilities of the db is restricted to stored procedures only.