Tuesday, 22 April 2014

HOW TO HIGHLIGHT THE DATE IN CALENDAR USING DATABASE ?

HOW TO HIGHLIGHT THE  DATE  IN CALENDAR USING DATABASE ?


DESCRIPTION:
 INN THIS POST I AM GOING TO TELL HOW TO HIGHLIGHT THE DATE 
IN CALENDER CONTROL USING SQL SERVER 


CREATE DATA BASE :

CREATE DATABASE EMP
USE EMP

CREATE TABLE EMP
(
      Task_Id int Identity(1,1),
      Task_Date Date,
    Posted_On Date
)


select * From EMP


Insert into EMP values('2014-04-30',GETDATE())
INSERT INTO  EMP VALUES('2014-07-21',GETDATE())
Insert into EMP values('2014-04-12',GETDATE())
Insert into EMP values('2014-04-06',GETDATE())


CREATE .ASPX :


<body>
    <form id="form1" runat="server">
    <div>
     <asp:Calendar ID="Calendar1" runat="server" SelectedDate=" " OnDayRender="Calendar1_DayRender" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">
        </asp:Calendar>
        <br />
        <br />
        <asp:Label ID="lblerrmsg" runat="server" ForeColor="Red"></asp:Label>
   
    </div>
    </form>
</body>


CREATE ASPX.CS :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected DataSet dsHolidays;
    //MySqlConnection objConn;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Calendar1.VisibleDate = DateTime.Today;
            FillHolidayDataset();
        }
    }

    protected void FillHolidayDataset()
    {
        DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year,
            Calendar1.VisibleDate.Month, 1);
        DateTime lastDate = GetFirstDayOfNextMonth();
        dsHolidays = GetCurrentMonthData(firstDate, lastDate);
    }

    protected DateTime GetFirstDayOfNextMonth()
    {
        int monthNumber, yearNumber;
        if (Calendar1.VisibleDate.Month == 12)
        {
            monthNumber = 1;
            yearNumber = Calendar1.VisibleDate.Year + 1;
        }
        else
        {
            monthNumber = Calendar1.VisibleDate.Month + 1;
            yearNumber = Calendar1.VisibleDate.Year;
        }
        DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
        return lastDate;
    }

    protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate)
    {
        DataSet dsMonth = new DataSet();

        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=EMP;Integrated Security=true;");
        String query = "SELECT Task_Date FROM EMP WHERE Task_Date >= @firstDate AND Task_Date < @lastDate";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.Add(new SqlParameter("@firstDate", firstDate));
        cmd.Parameters.Add(new SqlParameter("@lastDate", lastDate));

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        try
        {
            da.Fill(dsMonth);
        }
        catch (Exception ex) { lblerrmsg.Text = ex.Message; }
        return dsMonth;
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime nextDate;
        if (dsHolidays != null)
        {
            foreach (DataRow dr in dsHolidays.Tables[0].Rows)
            {
                nextDate = (DateTime)dr["Task_Date"];
                if (nextDate == e.Day.Date)
                {
                    e.Cell.BackColor = System.Drawing.Color.Pink;
                }
            }
        }
    }
    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
    {
        FillHolidayDataset();
    }

}



 OUTPUT:




No comments:

Post a Comment

Receive All Free Updates Via Facebook.