Hi Sofia,
Refer below code.
HTML
<form id="form1" runat="server">
Date:<asp:Label ID="lblDate" runat="server" />
<br />
Time:<asp:Label ID="lblTime" runat="server" />
</form>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Get Datetime from database.
string datetime = GetData();
lblDate.Text = Convert.ToDateTime(datetime).ToShortDateString();
lblTime.Text = Convert.ToDateTime(datetime).ToShortTimeString();
}
}
private string GetData()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT 'May 25 2021 3:14PM'", con))
{
con.Open();
string datetime = Convert.ToString(cmd.ExecuteScalar());
con.Close();
return datetime;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim datetime As String = GetData()
lblDate.Text = Convert.ToDateTime(datetime).ToShortDateString()
lblTime.Text = Convert.ToDateTime(datetime).ToShortTimeString()
End If
End Sub
Private Function GetData() As String
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT 'May 25 2021 3:14PM'", con)
con.Open()
Dim datetime As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
Return datetime
End Using
End Using
End Function
Screenshot