Hi, I am a newbie in using ASP.NET 2.0 and ADO.NET. I wrote a hangman game and want to record statistics at the end of each game. I will create and update records in the database for each authenticated user as well as a record for the Anonymous, unauthenticated user. After a win or loss has occurred, I want to programmatically use the SQLDataSource control to increment the statistics counters for the appropriate record in the database (note I don't want to show anything or get user input for this function).
I need a VB.NET codebehind example that will show me how I should set up the parameters and update the appropriate record in the database. Below is my code. What happens now is that the program chugs along happily (no errors), but the database record does not actually get updated. I have done many searches on this forum and on the general Internet for programmatic examples of an update sequence of code. If there is a tutorial for this online or a book, I'm happy to check it out.
Any help will be greatly appreciated.
Lambanlaa
CODE - Hangman.aspx.vb
1 Protected Sub UpdateStats()
2 Dim playeridString As String
3 Dim gamesplayedInteger, gameswonInteger, _
4 easygamesplayedInteger, easygameswonInteger, _
5 mediumgamesplayedInteger, mediumgameswonInteger, _
6 hardgamesplayedInteger, hardgameswonInteger As Int32
7
8 ' determine whether player is named or anonymous
9 If User.Identity.IsAuthenticated Then
10 Profile.Item("hangmanplayeridString") = User.Identity.Name
11 Else
12 Profile.Item("hangmanplayeridString") = "Anonymous"
13 End If
14
15 playeridString = Profile.Item("hangmanplayeridString")
16
17 ' look up record in stats database
18 Dim hangmanstatsDataView As System.Data.DataView = CType(statsSqlDataSource.Select(DataSourceSelectArguments.Empty), System.Data.DataView)
19
20 gamesplayedInteger = 0
21 gameswonInteger = 0
22 easygamesplayedInteger = 0
23 easygameswonInteger = 0
24 mediumgamesplayedInteger = 0
25 mediumgameswonInteger = 0
26 hardgamesplayedInteger = 0
27 hardgameswonInteger = 0
28
29 If hangmanstatsDataView.Table.Rows.Count = 0 Then
30
31 ' then create record with 0 values
32 statsSqlDataSource.InsertParameters.Clear() ' don't really know what Clear does
33 statsSqlDataSource.InsertParameters("playerid").DefaultValue = playeridString
34 statsSqlDataSource.InsertParameters("GamesPlayed").DefaultValue = gamesplayedInteger
35 statsSqlDataSource.InsertParameters("GamesWon").DefaultValue = gameswonInteger
36 statsSqlDataSource.InsertParameters("EasyGamesPlayed").DefaultValue = easygamesplayedInteger
37 statsSqlDataSource.InsertParameters("EasyGamesWon").DefaultValue = easygameswonInteger
38 statsSqlDataSource.InsertParameters("MediumGamesPlayed").DefaultValue = mediumgamesplayedInteger
39 statsSqlDataSource.InsertParameters("MediumGamesWon").DefaultValue = mediumgameswonInteger
40 statsSqlDataSource.InsertParameters("HardGamesPlayed").DefaultValue = hardgamesplayedInteger
41 statsSqlDataSource.InsertParameters("HardGamesWon").DefaultValue = hardgameswonInteger
42
43 statsSqlDataSource.Insert()
44 End If
45
46 ' reread the record to get current values
47 hangmanstatsDataView = CType(statsSqlDataSource.Select(DataSourceSelectArguments.Empty), System.Data.DataView)
48 Dim hangmanstatsDataRow As System.Data.DataRow = hangmanstatsDataView.Table.Rows.Item(0)
49
50 ' set temp variables to database values
51 gamesplayedInteger = hangmanstatsDataRow("GamesPlayed")
52 gameswonInteger = hangmanstatsDataRow("GamesWon")
53 easygamesplayedInteger = hangmanstatsDataRow("EasyGamesPlayed")
54 easygameswonInteger = hangmanstatsDataRow("EasyGamesWon")
55 mediumgamesplayedInteger = hangmanstatsDataRow("MediumGamesPlayed")
56 mediumgameswonInteger = hangmanstatsDataRow("MediumGamesWon")
57 hardgamesplayedInteger = hangmanstatsDataRow("HardGamesPlayed")
58 hardgameswonInteger = hangmanstatsDataRow("HardGamesWon")
59
60 ' update stats record
61 'statsSqlDataSource.UpdateParameters.Clear()
62 'statsSqlDataSource.UpdateParameters("playerid").DefaultValue = playeridString
63
64 If Profile.Item("hangmanwinorloseString") = "win" Then
65
66 statsSqlDataSource.UpdateParameters("GamesPlayed").DefaultValue = gamesplayedInteger + 1
67 statsSqlDataSource.UpdateParameters("GamesWon").DefaultValue = gameswonInteger + 1
68 Select Case Profile.Item("hangmandifficultyInteger")
69 Case 1
70 statsSqlDataSource.UpdateParameters("EasyGamesPlayed").DefaultValue = easygamesplayedInteger + 1
71 statsSqlDataSource.UpdateParameters("EasyGamesWon").DefaultValue = easygameswonInteger + 1
72 Case 2
73 statsSqlDataSource.UpdateParameters("MediumGamesPlayed").DefaultValue = mediumgamesplayedInteger + 1
74 statsSqlDataSource.UpdateParameters("MediumGamesWon").DefaultValue = mediumgameswonInteger + 1
75 Case 3
76 statsSqlDataSource.UpdateParameters("HardGamesPlayed").DefaultValue = hardgamesplayedInteger + 1
77 statsSqlDataSource.UpdateParameters("HardGamesWon").DefaultValue = hardgameswonInteger + 1
78 End Select
79
80
81 ElseIf Profile.Item("hangmanwinorloseString") = "lose" Then
82
83 statsSqlDataSource.UpdateParameters("GamesPlayed").DefaultValue = gamesplayedInteger + 1
84 Select Case Profile.Item("hangmandifficultyInteger")
85 Case 1
86 statsSqlDataSource.UpdateParameters("EasyGamesPlayed").DefaultValue = easygamesplayedInteger + 1
87 Case 2
88 statsSqlDataSource.UpdateParameters("MediumGamesPlayed").DefaultValue = mediumgamesplayedInteger + 1
89 Case 3
90 statsSqlDataSource.UpdateParameters("HardGamesPlayed").DefaultValue = hardgamesplayedInteger + 1
91 End Select
92 End If
93
94 statsSqlDataSource.Update()
95
96 End Sub
97
CODE - Hangman.aspx
1 <asp:SqlDataSource ID="statsSqlDataSource" runat="server" ConflictDetection="overwritechanges" 2 ConnectionString="<%$ ConnectionStrings:lambanConnectionString %>" DeleteCommand="DELETE FROM [Hangman_Stats] WHERE [PlayerID] = @original_PlayerID AND [GamesPlayed] = @original_GamesPlayed AND [GamesWon] = @original_GamesWon AND [EasyGamesPlayed] = @original_EasyGamesPlayed AND [EasyGamesWon] = @original_EasyGamesWon AND [MediumGamesPlayed] = @original_MediumGamesPlayed AND [MediumGamesWon] = @original_MediumGamesWon AND [HardGamesPlayed] = @original_HardGamesPlayed AND [HardGamesWon] = @original_HardGamesWon" 3 InsertCommand="INSERT INTO [Hangman_Stats] ([PlayerID], [GamesPlayed], [GamesWon], [EasyGamesPlayed], [EasyGamesWon], [MediumGamesPlayed], [MediumGamesWon], [HardGamesPlayed], [HardGamesWon]) VALUES (@PlayerID, @GamesPlayed, @GamesWon, @EasyGamesPlayed, @EasyGamesWon, @MediumGamesPlayed, @MediumGamesWon, @HardGamesPlayed, @HardGamesWon)" 4 OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT PlayerID, GamesPlayed, GamesWon, EasyGamesPlayed, EasyGamesWon, MediumGamesPlayed, MediumGamesWon, HardGamesPlayed, HardGamesWon FROM Hangman_Stats WHERE (PlayerID = @playerid)" 5 UpdateCommand="UPDATE [Hangman_Stats] SET [GamesPlayed] = @GamesPlayed, [GamesWon] = @GamesWon, [EasyGamesPlayed] = @EasyGamesPlayed, [EasyGamesWon] = @EasyGamesWon, [MediumGamesPlayed] = @MediumGamesPlayed, [MediumGamesWon] = @MediumGamesWon, [HardGamesPlayed] = @HardGamesPlayed, [HardGamesWon] = @HardGamesWon WHERE [PlayerID] = @original_PlayerID AND [GamesPlayed] = @original_GamesPlayed AND [GamesWon] = @original_GamesWon AND [EasyGamesPlayed] = @original_EasyGamesPlayed AND [EasyGamesWon] = @original_EasyGamesWon AND [MediumGamesPlayed] = @original_MediumGamesPlayed AND [MediumGamesWon] = @original_MediumGamesWon AND [HardGamesPlayed] = @original_HardGamesPlayed AND [HardGamesWon] = @original_HardGamesWon"> 6 <DeleteParameters> 7 <asp:Parameter Name="original_PlayerID" Type="String" /> 8 <asp:Parameter Name="original_GamesPlayed" Type="Int32" /> 9 <asp:Parameter Name="original_GamesWon" Type="Int32" /> 10 <asp:Parameter Name="original_EasyGamesPlayed" Type="Int32" /> 11 <asp:Parameter Name="original_EasyGamesWon" Type="Int32" /> 12 <asp:Parameter Name="original_MediumGamesPlayed" Type="Int32" /> 13 <asp:Parameter Name="original_MediumGamesWon" Type="Int32" /> 14 <asp:Parameter Name="original_HardGamesPlayed" Type="Int32" /> 15 <asp:Parameter Name="original_HardGamesWon" Type="Int32" /> 16 </DeleteParameters> 17 <UpdateParameters> 18 <asp:Parameter Name="GamesPlayed" Type="Int32" /> 19 <asp:Parameter Name="GamesWon" Type="Int32" /> 20 <asp:Parameter Name="EasyGamesPlayed" Type="Int32" /> 21 <asp:Parameter Name="EasyGamesWon" Type="Int32" /> 22 <asp:Parameter Name="MediumGamesPlayed" Type="Int32" /> 23 <asp:Parameter Name="MediumGamesWon" Type="Int32" /> 24 <asp:Parameter Name="HardGamesPlayed" Type="Int32" /> 25 <asp:Parameter Name="HardGamesWon" Type="Int32" /> 26 <asp:Parameter Name="original_PlayerID" Type="String" /> 27 <asp:Parameter Name="original_GamesPlayed" Type="Int32" /> 28 <asp:Parameter Name="original_GamesWon" Type="Int32" /> 29 <asp:Parameter Name="original_EasyGamesPlayed" Type="Int32" /> 30 <asp:Parameter Name="original_EasyGamesWon" Type="Int32" /> 31 <asp:Parameter Name="original_MediumGamesPlayed" Type="Int32" /> 32 <asp:Parameter Name="original_MediumGamesWon" Type="Int32" /> 33 <asp:Parameter Name="original_HardGamesPlayed" Type="Int32" /> 34 <asp:Parameter Name="original_HardGamesWon" Type="Int32" /> 35 </UpdateParameters> 36 <InsertParameters> 37 <asp:Parameter Name="PlayerID" Type="String" /> 38 <asp:Parameter Name="GamesPlayed" Type="Int32" /> 39 <asp:Parameter Name="GamesWon" Type="Int32" /> 40 <asp:Parameter Name="EasyGamesPlayed" Type="Int32" /> 41 <asp:Parameter Name="EasyGamesWon" Type="Int32" /> 42 <asp:Parameter Name="MediumGamesPlayed" Type="Int32" /> 43 <asp:Parameter Name="MediumGamesWon" Type="Int32" /> 44 <asp:Parameter Name="HardGamesPlayed" Type="Int32" /> 45 <asp:Parameter Name="HardGamesWon" Type="Int32" /> 46 </InsertParameters> 47 <SelectParameters> 48 <asp:ProfileParameter Name="playerid" PropertyName="hangmanplayeridString" /> 49 </SelectParameters> 50 </asp:SqlDataSource>
![]() |
0 |
![]() |
Hi lambanlaa,
I guess I've understood what you want to impliment in your program. As to the data access issue
There are servera ways you can follow:What happens now is that the program chugs along happily (no errors), but the database record does not actually get updated1.Check if you have written the correct connection string and the connection string has connected to your database? An easy way to identify why your programm doesnot work is to comment your existed code and write some "sample"/test code, which will perform a data access to your database(if it does effect your database, then something wrong in your code, if not, you must have missed something in your connection string,etc). Anyway, I just don't think it's too much difficult to solve this issue.
2.Set a breakpoint on SqlDataSource.update() to check for the 2 local variables: (i) updatestatement--make sure your update statements is executable (you can try it using query analyzer directly).
(ii) Profile.Item("hangmanwinorloseString")--check if the value has been set either "win" or "lose". if not,your statistic won't be saved.BTW, as to the resources on this area, first i would suggest you reading the msdn docments if you like, http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.80).aspx
and if you need further, check the book <essential ado.net > by Bob Beauchemin. Hope my suggestion helps
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
![]() |
0 |
![]() |
Thank you, Bo, this gives me a few ideas for testing.
Lambanlaa
![]() |
0 |
![]() |