Hi this is my first article on C# corner. I have some good topics in my mind, stay tuned.
In this article, I explain how to auto-refresh data on an ASP.NET page after a certain interval using AJAX UpdatePanel and other controls. I am using some AJAX controls and using a SQL Server database and a Data Grid control. The Database name is Northwind. In this application my interval time for refresh data is 30 seconds. You can change your time by the times interval property.
Here is a snapshot:

This code is for binding data:
public void BindData()
{
con = new SqlConnection("Initial Catalog=Northwind; Data Source=localhost; Uid=sa; pwd=;");
cmd.CommandText = "select * from Employees ";
cmd.Connection = con;
con.Open();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
cmd.ExecuteNonQuery();
GridData.DataSource = ds;
GridData.DataBind();
}
You can check your current time on page load. Write this code:
MyLabel.Text = System.DateTime.Now.ToString();
BindData();
And the grid refresh time is:
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();
}
Here is the HTML design code:
<form id="form1" runat="server">
<asp:Label ID="Label2" runat="server" Text="This is Time, When The Full Page Load :" Font-Bold="true"></asp:Label>
<asp:Label ID="MyLabel" runat="server"></asp:Label><br /><br />
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="30000">

Faraz AhmededitedPosted Jun 15, 2010, 11:12 AMEdited Feb 9, 2011, 11:55 PM
Thanks for this. It was great help!