Hi nauna,
Refer below sample.
SQL
CREATE TABLE tblTimes
(
Id INT IDENTITY PRIMARY KEY,
StartTime VARCHAR(10),
EndTime VARCHAR(10)
)
INSERT INTO tblTimes VALUES('12:00 PM','02:00 PM')
SELECT * FROM tblTimes
Namepsaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = GetData();
DateTime dtStart = Convert.ToDateTime(dt.Rows[0]["StartTime"]);
DateTime dtEnd = Convert.ToDateTime(dt.Rows[0]["EndTime"]);
DateTime current = DateTime.Now;
if (current.Ticks > dtStart.Ticks && current.Ticks < dtEnd.Ticks)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Not available')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Available')", true);
}
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM tblTimes";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = GetData()
Dim dtStart As DateTime = Convert.ToDateTime(dt.Rows(0)("StartTime"))
Dim dtEnd As DateTime = Convert.ToDateTime(dt.Rows(0)("EndTime"))
Dim current As DateTime = DateTime.Now
If current.Ticks > dtStart.Ticks AndAlso current.Ticks < dtEnd.Ticks Then
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Not available')", True)
Else
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Available')", True)
End If
End Sub
Private Function GetData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM tblTimes"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function