Windows Develop Bookmark and Share   
 index > Windows Forms General > How to display data from database one after another in Timer tick event
 

How to display data from database one after another in Timer tick event

hi,

i have a situation where i need to display rows of data from a DB to a textbox, one row at a time. The requirement needs me to change the data every after 30seconds delay. So, i decided to use a timer control in order to create the delay. But how can i go from there? How can i iterate through the rows of data just to show only one row at a time when the timer's tick event invoked? Thanks in advance.

shahrul  Friday, November 23, 2007 4:12 AM

Hi, shahrul,

Based on my understanding, you want to get therecords from your DataBase and display them one by one with a timer, don't you?

In my point of view, you can get all the records into a DataTable, and display the rows one by one.

For example,

Code Block

DataTable dataTable;

Timer timer;

int index = 0;

private void Form1_Load(object sender, EventArgs e)

{

OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.newConnectionString);

OleDbDataAdapter adapter = new OleDbDataAdapter("select * from Table1", conn);

dataTable = new DataTable();

adapter.Fill(dataTable); //Get Data from the DataBase, I use an Access as an example

adapter.Dispose();

adapter = null;

conn.Close();

conn = null;

timer = new Timer();

timer.Interval = 30000; //The timer is triggered every 30 seconds

timer.Tick += new EventHandler(timer_Tick);

timer.Start();

}

void timer_Tick(object sender, EventArgs e)

{

if (index < dataTable.Rows.Count)

{

textBox1.Clear();

foreach (DataColumn column in dataTable.Columns)

{

textBox1.Text += dataTable.Rows[index][column].ToString() + " "; //Show the row in the TextBox

}

index++;

}

else

{

timer.Stop();

}

}

Hi, shahrul,

Based on my understanding, you want to get therecords from your DataBase and display them one by one with a timer, don't you?

In my point of view, you can get all the records into a DataTable, and display the rows one by one.

For example,

Code Block

DataTable dataTable;

Timer timer;

int index = 0;

private void Form1_Load(object sender, EventArgs e)

{

OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.newConnectionString);

OleDbDataAdapter adapter = new OleDbDataAdapter("select * from Table1", conn);

dataTable = new DataTable();

adapter.Fill(dataTable); //Get Data from the DataBase, I use an Access as an example

adapter.Dispose();

adapter = null;

conn.Close();

conn = null;

timer = new Timer();

timer.Interval = 30000; //The timer is triggered every 30 seconds

timer.Tick += new EventHandler(timer_Tick);

timer.Start();

}

void timer_Tick(object sender, EventArgs e)

{

if (index < dataTable.Rows.Count)

{

textBox1.Clear();

foreach (DataColumn column in dataTable.Columns)

{

textBox1.Text += dataTable.Rows[index][column].ToString() + " "; //Show the row in the TextBox

}

index++;

}

else

{

timer.Stop();

}

}

hi yu gou, thanks man. your code really works for me.

shahrul  Monday, December 10, 2007 10:29 AM
I was really glad I came across this bit of code you'd posted, quite helpful! I was just wondering, however, what if you had a situation where you wanted it to go back to the beginning and loop through it all again?

I am trying to make a sort of slide show with Ajax using the timer_tick event associated with an update panel. This is what I have at the moment, but I am running into issues with getting it to "start over".

Thanks!


Code Snippet

private int n
{
get { return (int)ViewState["n_value"]; }
set { ViewState["n_value"] = value; }
}

private void GetEventData(int eventID) }

{

GetItem newEvent = new GetItem(Dbname);

dlNewEvent.DataSource = newEvent.GetNewEvent(eventID);

dlNewEvent.DataBind();

}


protected void Timer1_Tick(object sender, EventArgs e)

{

if (n < dt.Rows.Count)

{

foreach (DataColumn column in dt.Columns)

{

GetEventData(Convert.ToInt32(dt.Rows[n][column].ToString()));

}

n++;

}




froglander  Monday, July 21, 2008 5:12 PM

Just follow the original code replacing Timer1_Tick with this:

Code Snippet

void timer_Tick(object sender, EventArgs e)

index = 0;

foreach (DataColumn column in dataTable.Columns)

You can use google to search for other answers

Custom Search

More Threads

• Accessing form controls in a class file
• Loosing display after compiling (form designer) when adding User Control
• Form Refresh Question
• creating mutiple worksheets in an xls using vb.net
• Process won't terminate, even with call to Environment.Exit()
• RichTextBox missing formatting when loading .rtf file
• Deployment error on windows 2000
• Problem with mixing CompactFramework and normal framework
• Custom tooltip form - stop it getting focus
• MessageBox buttons localization