Hey arie.keren,
Write your database code on Page_Load then lblWaktu will get time from databse and put your label in session get this session value onTick event.
Refer below sample.
HTML
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList runat="server" ID="ddlId">
<asp:ListItem Text="10249" />
<asp:ListItem Text="10250" />
</asp:DropDownList>
<p>
Waktu Test :
<asp:Label ID="lblWaktu" runat="server" Text="Label"></asp:Label>
Menit</p>
<asp:Timer ID="Timer1" runat="server" OnTick="GetTime" Interval="1000" />
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
string sqlString = "select ShipCity, OrderDate from Orders ";
sqlString = sqlString + "where OrderId = " + ddlId.SelectedItem.Text;
SqlCommand myCommand = new SqlCommand(sqlString, con);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
DateTime dt = Convert.ToDateTime(myReader["OrderDate"]);
lblWaktu.Text = dt.ToString("mm");
Session["timeout"] = dt;
}
con.Close();
}
protected void GetTime(object sender, EventArgs e)
{
TimeSpan time1 = new TimeSpan();
time1 = (DateTime)Session["timeout"] - DateTime.Now;
if (time1.Seconds <= 0)
{
//plhPilihan.Visible = true;
//plhTest.Visible = false;
}
else
{
lblWaktu.Text = time1.Seconds.ToString();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
con.Open()
Dim sqlString As String = "select ShipCity, OrderDate from Orders "
sqlString = sqlString & "where OrderId = " & ddlId.SelectedItem.Text
Dim myCommand As SqlCommand = New SqlCommand(sqlString, con)
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
If myReader.Read() Then
Dim dt As DateTime = Convert.ToDateTime(myReader("OrderDate"))
lblWaktu.Text = dt.ToString("mm")
Session("timeout") = dt
End If
con.Close()
End Sub
Protected Sub GetTime(ByVal sender As Object, ByVal e As EventArgs)
Dim time1 As TimeSpan = New TimeSpan()
time1 = CType(Session("timeout"), DateTime) - DateTime.Now
If time1.Seconds <= 0 Then
Else
lblWaktu.Text = time1.Seconds.ToString()
End If
End Sub