Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

How to bind the database values in labels with refresh on every 30 seconds in asp.net from sql server.
Posted

Check out this link

http://stackoverflow.com/a/5052661/312219[^]

You basically have to give a $.get / $.ajax request every 30 seconds to a url which would return the data to be displayed in the label.
 
Share this answer
 
Comments
Tim Corey 22-Jun-12 21:36pm    
Good answer. I would definitely recommend a jQuery ajax call so that the refresh isn't a full page refresh.
Pankaj Nikam 23-Jun-12 2:56am    
+5 Great answer. JQuery rocks!
Hi ,
Check this
XML
<div>
     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
     <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="30000">
     </asp:Timer>
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
               <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           </ContentTemplate>
               <Triggers>

                   <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

               </Triggers>
     </asp:UpdatePanel>
 </div>


C#
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Timer1_Tick(object sender, EventArgs e)
{
     // bring the Data from Database 
    //Label1.Text = DateTime.Now.ToString();
}

Best Regards
M.Mitwalli
 
Share this answer
 
Souce Code .aspx code:

XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 343px;
        }
    </style>

</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
     <asp:Timer ID="Timer1" runat="server"  Interval="30000" ontick="Timer1_Tick">
     </asp:Timer>
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
               <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           </ContentTemplate>
               <Triggers>

                   <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

               </Triggers>
     </asp:UpdatePanel>
 </div>
    </form>
</body>
</html>

-------------------------------------------------------------------------------
C# Code:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}
protected void Timer1_Tick(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("here ur database connection string");
DataSet ds = new DataSet();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select Id from Table", con);
da.Fill(ds);
con.Close();
int k = 0;
k = Convert.ToInt32(ViewState["k"]);
if (k < ds.Tables[0].Rows.Count)
{
ViewState["k"] = k;
ViewState["k"] = Convert.ToInt32(ViewState["k"]) + 1;
Label1.Text = ds.Tables[0].Rows[k][0].ToString();
}

}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900