dotnettechy.com Your Ad Here

Tuesday, June 16, 2009

Auto Refresh data on page using AJAX

Hi this is my first article on C# corner. I have some good topics in my mind, stay tuned.
In this article, I am going to explain how we can 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 SQL server database and Data Grid control. Database name is north wind. In this application my interval time for refresh data is 30 second. You can change your time by times interval property.
Here is a snapshot: 
This code for bind data:

Public Sub 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()
End Sub
You can check your current time on page load. Write this code:

MyLabel.Text = System.DateTime.Now.ToString()
BindData()
and grid refresh time is:

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString()
End Sub
Here is HTML desing code:
See full detail: http://www.vbdotnetheaven.com/UploadFile/raj1979/ac03192009014403AM/ac.aspx

No comments: