Hi sani.ss501,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox ID="txtEventDesc" runat="server" />
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Clicked" OnDayRender="Calendar1_DayRender"
ShowGridLines="True"></asp:Calendar>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Code
C#
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
System.Data.DataTable dt = GetEvents();
foreach (System.Data.DataRow row in dt.Rows)
{
if (Convert.ToDateTime(e.Day.Date) == Convert.ToDateTime(row["Date"]))
{
e.Cell.Controls.Add(new Label { Text = "<br/>" });
e.Cell.Controls.Add(new Label { Text = row["Desc"].ToString() });
}
}
}
private System.Data.DataTable GetEvents()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Desc,Date FROM Events";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void Clicked(object sender, EventArgs e)
{
string desc = GetEventDesc((sender as Calendar).SelectedDate);
if (string.IsNullOrEmpty(desc))
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Events VALUES(@Desc,@Date)";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Desc", txtEventDesc.Text);
cmd.Parameters.AddWithValue("@Date", (sender as Calendar).SelectedDate);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + desc + "')", true);
}
}
private string GetEventDesc(DateTime eventDate)
{
string eventDesc = "";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Desc FROM Events WHERE Date=@Date";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Date", eventDate);
con.Open();
eventDesc = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
return eventDesc;
}
VB.Net
Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
Dim dt As System.Data.DataTable = GetEvents()
For Each row As System.Data.DataRow In dt.Rows
If Convert.ToDateTime(e.Day.Date) = Convert.ToDateTime(row("Date")) Then
e.Cell.Controls.Add(New Label With {.Text = "<br/>"})
e.Cell.Controls.Add(New Label With {.Text = row("Desc").ToString()})
End If
Next
End Sub
Private Function GetEvents() As System.Data.DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT Desc,Date FROM Events"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
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
Protected Sub Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim desc As String = GetEventDesc((TryCast(sender, Calendar)).SelectedDate)
If String.IsNullOrEmpty(desc) Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO Events VALUES(@Desc,@Date)"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Desc", txtEventDesc.Text)
cmd.Parameters.AddWithValue("@Date", (TryCast(sender, Calendar)).SelectedDate)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Else
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" & desc & "')", True)
End If
End Sub
Private Function GetEventDesc(ByVal eventDate As DateTime) As String
Dim eventDesc As String = ""
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT Desc FROM Events WHERE Date=@Date"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Date", eventDate)
con.Open()
eventDesc = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
Return eventDesc
End Function