Refresh page based on Database value using JavaScript

Last Reply on May 03, 2018 05:12 AM By kalpesh

Posted on May 03, 2018 04:17 AM

Dear All,

I'm using the below script to refresh the page after 30 mins but i want to refresh the page after getting the value from the database query.

 

<script language="javascript" type="text/javascript">
        function startTimer() {
            var now = new Date();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            setTimeout('refresh()', (((30 - (minutes % 30) - ((seconds > 0) ? 1 : 0)) * 60) + (60 - seconds)) * 1000);
        }

        function refresh() {
            window.location.href = 'Dashboard.aspx';
//           alert('page refreshed');
        }

        startTimer();
</script>

 Here is my query and the output is 102Minutes.

select DATEDIFF(MINUTE,GETDATE(),'2018-05-03 14:00:00.000'),MeetingTitle from [status_meeting1] where HasExpiredView=2

Can you please let me know how can i refresh the page after the certain time value from the query.

thanks in advance

You are viewing reply posted by: kalpesh on May 03, 2018 05:12 AM.
Posted on May 03, 2018 05:12 AM Modified on on May 03, 2018 05:13 AM

Refer below Sample code for your reference and implement it as per your code logic.

HTML

C#

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function startTimer() {
            var now = new Date();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            var refreshtime = '<%=this.RefreshTime %>';
            if (parseInt(refreshtime) > 0) {
                setTimeout('refresh()', (((refreshtime - (minutes % refreshtime) - ((seconds > 0) ? 1 : 0)) * 60) + (60 - seconds)) * 1000);
            }
        }

        function refresh() {
            window.location.href = 'Default.aspx';
        }

        startTimer();
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

VB.Net

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function startTimer() {
            var now = new Date();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            var refreshtime = '<%= Me.RefreshTime %>';
            if (parseInt(refreshtime) > 0) {
                setTimeout('refresh()', (((refreshtime - (minutes % refreshtime) - ((seconds > 0) ? 1 : 0)) * 60) + (60 - seconds)) * 1000);
            }
        }
        function refresh() {
            window.location.href = 'VB.aspx';
        }
        startTimer();
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

Code

C#

public int RefreshTime { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        // call you code to set the Property value
        this.RefreshTime = GetRefreshTime();
    }
}

private int GetRefreshTime()
{
    int refreshTime = 0;
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    // Your Sql Query as per your logic.
    string query = "select DATEDIFF(MINUTE,GETDATE(),'2018-05-03 18:00:00.000')";
    SqlCommand cmd = new SqlCommand(query);
    SqlConnection con = new SqlConnection(conString);
    cmd.Connection = con;
    con.Open();
    refreshTime = Convert.ToInt32(cmd.ExecuteScalar());
    con.Close();
    return refreshTime;
}

VB.Net

Public Property RefreshTime As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.RefreshTime = GetRefreshTime()
    End If
End Sub

Private Function GetRefreshTime() As Integer
    Dim refreshTime As Integer = 0
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "select DATEDIFF(MINUTE,GETDATE(),'2018-05-03 18:00:00.000')"
    Dim cmd As SqlCommand = New SqlCommand(query)
    Dim con As SqlConnection = New SqlConnection(conString)
    cmd.Connection = con
    con.Open()
    refreshTime = Convert.ToInt32(cmd.ExecuteScalar())
    con.Close()
    Return refreshTime
End Function