Hello everyone,
I am trying to write a script that will rotate the news in one <div> with the use of ajax timer. I am trying to use a public variable named "num" in order to have different news shown with every tick event. The problem is that when I run the program, the browser shows the first news, then changes to the second one, and then afterwards the variable "num" never increments. I have added a label which receives the current time (Label1.Text = DateTime.Now.ToString();) with every tick and that works properly, but when I did the same with the num variable, I have seen that the number increments to number 2 and then stops incrementing.
This is the code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class RotatingNews : System.Web.UI.Page
{
public int num = 1;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.;Initial Catalog=Knjige;Integrated Security=True";
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "Select Naslov, Tekst from Vesti where id = " + num;
SqlDataReader rdr;
cn.Open();
rdr = cm.ExecuteReader();
Repeater1.DataSource = rdr;
Repeater1.DataBind();
rdr.Close();
cn.Close();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
broj++;
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.;Initial Catalog=Knjige;Integrated Security=True";
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "Select Naslov, Tekst from Vesti where id = " + broj;
SqlDataReader rdr;
cn.Open();
rdr = cm.ExecuteReader();
Repeater1.DataSource = rdr;
Repeater1.DataBind();
rdr.Close();
cn.Close();
Label1.Text = broj.ToString();
Label2.Text = DateTime.Now.ToString();
}
}
Does anybody have an idea why this is not working properly?