AutoRefresh the grid data at certain time of interval

By SP

Here is the code to refresh the data of gridview/datagrid at certain time of interval using ajax and update palel.

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:
<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">
            </asp:Timer>
        </div>
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
            <ContentTemplate>
                <asp:Label ID="Label3" runat="server" Text="This is The Time when Only Data Grid will Referesh :" Font-Bold="true"></asp:Label> 
                <asp:Label ID="Label1" runat="server" Text="Grid not refreshed yet."></asp:Label><br />
                <asp:Label ID="Label4" runat="server" Text="(Grid Will Referesh after Every 30 Sec)" Font-Bold="true"></asp:Label> 
                <br /><br />
                <asp:DataGrid ID=GridData runat="server" Width="100%" GridLines="Both" HeaderStyle-BackColor="#999999" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundColumn DataField="EmployeeID" HeaderText="Employee ID"></asp:BoundColumn>
                        <asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
                         <asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
                        <asp:BoundColumn DataField="City" HeaderText="City"></asp:BoundColumn>
                    </Columns>
                </asp:DataGrid>
                  
            </ContentTemplate>
        </asp:UpdatePanel>    
    
    </form>

AutoRefresh the grid data at certain time of interval  (1252 Views)