Hello,
I am implementing a timer as part of a chat functionality on my website with code I hand rolled. What it is supposed to is every 3 seconds it updates the members list so that after x amount of time it will remove members that are inactive from the list of who is in the room. It also updates the chat window for new messages posted by others. The Chat window is pulled out of a database for the last 100 messages. The database table is updated from a button when the user types it in one record per entry. Eventually I plan on purging the database table once a day to a file....
The situation I am having is as soon as I add the timer to do the updating, the retrieving of the chat window lines gets mixed up and not always placed in order of when inserted into the database, and sometimes it didnt even make it into the database... I have tried many routines trying to fix this over the past week but can not figure out what I am doing wrong....Help please
Here is my .ASP
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="SpadesRoom1.aspx.vb" Inherits="Spades_Room1" %><asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"><style type="text/css"> .style137 { width: 952px; } .style138 { width: 214px; } .style139 { height: 25px; } .style140 { height: 25px; width: 184px; } .style142 { height: 25px; } .style143 { height: 25px; width: 160px; }</style></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"><asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager><asp:Timer ID="Timer1" runat="server" Interval="3600" ontick="Timer1_Tick"></asp:Timer><asp:Panel ID="Roomlist" runat="server"><br /><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT DISTINCT [PlayerName], [Rating] FROM [Spades]"></asp:SqlDataSource> BANNER AD WILL GO HERE<br /><table ID="SeBkt" runat="server" align="left" class="style137" frame="border" rules="all" style="background-color: #C0C0C0"><tr ID="HeadRow" runat="server"><td class="style142"> </td><td class="style142" colspan="4"> </td><td class="style138"> </td></tr><tr ID="row0" runat="server"><td bgcolor="White" class="style139"> TB # </td><td align="left" bgcolor="White" class="style139"> playing or Join</td><td bgcolor="White" class="style140"> playing or Join</td><td bgcolor="White" class="style140"> playing or Join</td><td bgcolor="White" class="style143"> playing or Join</td><td bgcolor="White" class="style138" rowspan="3" valign="top"> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" PageSize="30"><Columns><asp:BoundField DataField="PlayerName" HeaderText="PlayerName" SortExpression="PlayerName" /><asp:BoundField DataField="Rating" HeaderText="Rating" SortExpression="Rating" /></Columns></asp:GridView> </td></tr><tr ID="row1" runat="server"><td bgcolor="#999999" class="style139" colspan="5"> </td></tr><tr ID="row2" runat="server"><td bgcolor="White" class="style142" colspan="5"><asp:TextBox ID="Chatbox0" runat="server" BorderStyle="Solid" Height="193px" ReadOnly="True" Rows="100" style="margin-top: 0px" TextMode="MultiLine" Width="725px">this is the chat box</asp:TextBox><script type="text/javascript"> window.onload = function () { var textarea = document.getElementById('<%=Chatbox0.ClientID %>'); textarea.scrollTop = textarea.scrollHeight; } </script><br /><asp:TextBox ID="LinetoSubmit" runat="server" BorderStyle="Solid" Height="40px" Width="650px">Enter something here</asp:TextBox><asp:Button ID="BtnSubmitChat" runat="server" Height="40px" Text="Submit " /></td></tr></table></asp:Panel><p><asp:TextBox ID="NotLoggedIn" runat="server" BorderStyle="None" Font-Bold="True" Font-Size="X-Large" ReadOnly="True" Width="766px">Sorry you need to be logged in to access this area</asp:TextBox></p><p><asp:DetailsView ID="Players" runat="server" AllowPaging="True" AutoGenerateRows="False" DataSourceID="SqlDataSource1" Height="50px" Width="125px" Visible="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True"><Fields><asp:BoundField DataField="PlayerName" HeaderText="PlayerName" SortExpression="PlayerName" /><asp:BoundField DataField="Rating" HeaderText="Rating" SortExpression="Rating" /><asp:BoundField DataField="LastPlayed" HeaderText="LastPlayed" SortExpression="LastPlayed" /><asp:BoundField DataField="GamesWon" HeaderText="GamesWon" SortExpression="GamesWon" /><asp:BoundField DataField="GamesLost" HeaderText="GamesLost" SortExpression="GamesLost" /><asp:BoundField DataField="OnTables" HeaderText="OnTables" SortExpression="OnTables" /><asp:BoundField DataField="InRooms" HeaderText="InRooms" SortExpression="InRooms" /></Fields></asp:DetailsView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Spades]"></asp:SqlDataSource><asp:DetailsView ID="ChatView" runat="server" AllowPaging="True" AutoGenerateRows="False" DataSourceID="SqlDataSource3" Height="50px" Width="125px"><Fields><asp:BoundField DataField="ChatFrom" HeaderText="ChatFrom" SortExpression="ChatFrom" /><asp:BoundField DataField="ChatMessage" HeaderText="ChatMessage" SortExpression="ChatMessage" /><asp:BoundField DataField="MessageTime" HeaderText="MessageTime" SortExpression="MessageTime" /><asp:BoundField DataField="PlayersInRoom" HeaderText="PlayersInRoom" SortExpression="PlayersInRoom" /></Fields></asp:DetailsView><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [SpadesRoom1Chat]"></asp:SqlDataSource><br /></p></asp:Content>
here is my codebehind
Partial Class Room1 Inherits System.Web.UI.Page Dim IsAPlayer As Boolean = False Dim getname As String = User.Identity.Name Dim Temp As String Dim ThePlayers As String Dim IntervalTime As Integer = 10000 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Session("chatinput") = LinetoSubmit.Text Players.PageIndex = 0 ChatView.PageIndex = ChatView.PageCount - 1 ChatView.DataBind() Players.DataBind() ThePlayers = ChatView.Rows.Item(3).Cells(1).Text If Not IsPostBack Then If User.Identity.IsAuthenticated = False Then Roomlist.Visible = False NotLoggedIn.Visible = True Else Roomlist.Visible = True NotLoggedIn.Visible = False 'Players.Visible = True AddPlayerToRoom() End If End If LoadPlayers() LoadChatWindow() LinetoSubmit.Focus() End Sub Public Sub AddPlayerToRoom() 'THIS SECTION ADDS A MEMBER TO THE ROOM End If End Sub Protected Sub BtnSubmitChat_Click(sender As Object, e As System.EventArgs) Handles BtnSubmitChat.Click 'UPDATE TIME OF LAST ACTIVITY ' PLAYER INFO FOR CSV STYLE USERNAME;RATING;LASTaCTIVITYtIMER, Dim SplitPlayer As Array Dim SplitInnerPlayer As Array Temp = "" SplitPlayer = Split(ThePlayers, ",") For x = 0 To SplitPlayer.Length - 1 SplitInnerPlayer = Split(SplitPlayer(x), ";") If SplitInnerPlayer(0) = getname Then SplitInnerPlayer(2) = "0" SplitPlayer(x) = SplitInnerPlayer(0) & ";" & SplitInnerPlayer(1) & ";" & SplitInnerPlayer(2) End If If Temp = "" Then Temp = SplitPlayer(x) Else Temp = Temp & "," & SplitPlayer(x) End If Next ThePlayers = Temp If LinetoSubmit.Text <> "" Then SqlDataSource3.InsertCommandType = SqlDataSourceCommandType.Text SqlDataSource3.InsertCommand = "INSERT INTO [SpadesRoom1Chat] ([ChatFrom],[ChatMessage],[MessageTime],[PlayersInRoom]) VALUES (@ChatFrom,@ChatMessage,@MessageTime,@PlayersInRoom)" SqlDataSource3.InsertParameters.Add("ChatFrom", getname) SqlDataSource3.InsertParameters.Add("ChatMessage", LinetoSubmit.Text) SqlDataSource3.InsertParameters.Add("MessageTime", Today) SqlDataSource3.InsertParameters.Add("PlayersInRoom", ThePlayers) SqlDataSource3.Insert() ChatView.DataBind() LinetoSubmit.Text = "" Session("chatinput") = LinetoSubmit.Text LoadChatWindow() End If End Sub Public Sub LoadChatWindow() Dim TempChat As String = "" 'Chatbox0.Text = "" If ChatView.PageCount < 100 Then For x = 0 To ChatView.PageCount - 1 ChatView.PageIndex = x ChatView.DataBind() If TempChat = "" Then TempChat = ChatView.Rows.Item(0).Cells(1).Text & ":>" & ChatView.Rows.Item(1).Cells(1).Text Else TempChat = TempChat & vbCrLf & ChatView.Rows.Item(0).Cells(1).Text & ": " & ChatView.Rows.Item(1).Cells(1).Text End If Next Else For x = ChatView.PageCount - 99 To ChatView.PageCount - 1 ChatView.PageIndex = x ChatView.DataBind() If TempChat = "" Then TempChat = ChatView.Rows.Item(0).Cells(1).Text & ": " & ChatView.Rows.Item(1).Cells(1).Text Else TempChat = TempChat & vbCrLf & ChatView.Rows.Item(0).Cells(1).Text & ": " & ChatView.Rows.Item(1).Cells(1).Text End If Next End If Chatbox0.Text = TempChat LinetoSubmit.Focus() End Sub Public Sub LoadPlayers() ' PLAYER INFO FOR CSV STYLE USERNAME;RATING;LASTACTIVE, Dim SplitPlayer As Array Dim SplitInnerPlayer As Array Dim LastActivityTimer As Integer ' Add 3 seconds to everyone Temp = "" SplitPlayer = Split(ThePlayers, ",") For x = 0 To SplitPlayer.Length - 1 SplitInnerPlayer = Split(SplitPlayer(x), ";") SplitInnerPlayer(2) = SplitInnerPlayer(2) + 3 SplitPlayer(x) = SplitInnerPlayer(0) & ";" & SplitInnerPlayer(1) & ";" & SplitInnerPlayer(2) LastActivityTimer = SplitInnerPlayer(2) If LastActivityTimer < 900 Then 'only keep in players in room if less than 15 minutes inactive If Temp = "" Then Temp = SplitPlayer(x) Else Temp = Temp & "," & SplitPlayer(x) End If End If Next ThePlayers = Temp 'Update Database for players SqlDataSource3.UpdateParameters.Clear() SqlDataSource3.UpdateCommandType = SqlDataSourceCommandType.Text SqlDataSource3.UpdateCommand = "UPDATE [SpadesRoom1Chat] SET [PlayersInRoom] = @PlayersInRoom" SqlDataSource3.UpdateParameters.Add("PlayersInRoom", ThePlayers) SqlDataSource3.Update() ChatView.DataBind() 'LoadChatWindow() GridView1.PageIndex = 0 GridView1.DataBind() End Sub Public Sub RemovePlayerFromRoom() Dim PlayerRemovingIs As String = getname Dim PlayerRemoved As Boolean = False ' PLAYER INFO FOR CSV STYLE USERNAME;RATING;LASTACTIVE, Dim SplitPlayer As Array Dim SplitInnerPlayer As Array Temp = "" SplitPlayer = Split(ThePlayers, ",") For x = 0 To SplitPlayer.Length - 1 SplitInnerPlayer = Split(SplitPlayer(x), ";") If SplitInnerPlayer(0) <> PlayerRemovingIs Then If Temp = "" Then Temp = SplitPlayer(x) Else Temp = Temp & "," & SplitPlayer(x) End If End If Next ThePlayers = Temp SqlDataSource3.InsertCommandType = SqlDataSourceCommandType.Text SqlDataSource3.InsertCommand = "INSERT INTO [SpadesRoom1Chat] ([ChatFrom],[ChatMessage],[MessageTime],[PlayersInRoom]) VALUES (@ChatFrom,@ChatMessage,@MessageTime,@PlayersInRoom)" SqlDataSource3.InsertParameters.Add("ChatFrom", "Zoombyya.com") SqlDataSource3.InsertParameters.Add("ChatMessage", getname & " has left the room") SqlDataSource3.InsertParameters.Add("MessageTime", Today) SqlDataSource3.InsertParameters.Add("PlayersInRoom", ThePlayers) SqlDataSource3.Insert() ChatView.DataBind() 'LinetoSubmit.Text = "" 'LoadChatWindow() End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound Dim SplitPlayer As Array Dim SplitInnerPlayer As Array Dim IsPlayerInRoom As String SplitPlayer = Split(ThePlayers, ",") For y = 0 To GridView1.PageCount - 1 For x = 0 To SplitPlayer.Length - 1 SplitInnerPlayer = Split(SplitPlayer(x), ";") IsPlayerInRoom = SplitInnerPlayer(0) If e.Row.RowType = DataControlRowType.DataRow Then If e.Row.Cells(0).Text <> IsPlayerInRoom Then e.Row.Visible = False Else e.Row.Visible = True 'e.Row.BackColor = System.Drawing.Color.Yellow 'e.Row.Cells(2).BackColor = Drawing.Color.Crimson End If End If Next Next End Sub Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick LinetoSubmit.Focus() End Sub Protected Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete LinetoSubmit.Text = Session("chatinput") End Sub End Class