Reputation: 7201
I have a label called lblMessage. The data of the message is saved in a SQL Server 2005 DB. In my table i have a message ID and then 4 rows with my message ID. Message 1, Message 2, Message 3 and Message 4.
I want my lblMessage to go through a loop every time the user refresh his browser, so that when he refresh it once the next message will be Message 2..... and so on and so on.
How would i do this in VB.NET or C#?
Thanks in advance!
Upvotes: 0
Views: 1117
Reputation: 1992
You didn't describe the table schema in your question, so I don't know if your message table contains anything other than a message ID and the message. If you can add another column, you could call it something like sequence and make it a tinyint.
Now, when fetching your message, you need to specify both the message id and the sequence number.
Every time you refresh, you increment the sequence number and get its modulo to 4 (so that sequence is always 0, 1, 2 or 3).
something like:
sequence = sequence++ % 4;
lblMessage.Text = fetchMessage(MessageID, sequence);
and fetchMessage simply performs your query:
Select message
From tbl_Messages
Where messageID = @messageID
and sequence = @sequence
Upvotes: 1
Reputation: 43875
VB.NET
Private Shared messageNumber as Integer
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Select Case messageNumber
Case 0
lblMessage.Text = MESSAGE_1
Case 1
lblMessage.Text = MESSAGE_2
Case 2
lblMessage.Text = MESSAGE_3
Case 3
lblMessage.Text = MESSAGE_4
End Select
messageNumber = (messageNumber + 1) Mod 4
End Sub
C#
private static int messageNumber;
public void Page_Load(object sender, System.EventArgs e)
{
switch (messageNumber) {
case 0:
lblMessage.Text = MESSAGE_1;
break;
case 1:
lblMessage.Text = MESSAGE_2;
break;
case 2:
lblMessage.Text = MESSAGE_3;
break;
case 3:
lblMessage.Text = MESSAGE_4;
break;
}
messageNumber = (messageNumber + 1) % 4;
}
Upvotes: 2