Hello Guys,
Below I have created one simple application form for view
and add entry for attendance system. This is for purpose to get attendance of
the employee on monthly basis and can view as well. This application has
following features:
User can add their entry on using customized calendar system
with label date and day
Can view their previously added entry
All entries will be stored in database
User can only add the current month entry and till current
date, no entry will be allowed beyond current date
No entry will be allowed on Sat-Sun
Entry will be allowed for current month and year, no entry
will be allowed for previous month, year and next month year
I hope you will be getting some useful code using this
Calendar Time Entry, please do post your comments and feedback on this.
Here is .aspx code
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="TimeSheet.aspx.cs"
Inherits="TimeSheet"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Calendar Time Entry</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlYear"
runat="server">
<asp:ListItem Value="0">Select
Year</asp:ListItem>
<asp:ListItem Value="2014">Year
2014</asp:ListItem>
<asp:ListItem Value="2015">Year
2015</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth"
runat="server">
<asp:ListItem Value="0">Select
Month</asp:ListItem>
<asp:ListItem Value="1">Jan</asp:ListItem>
<asp:ListItem Value="2">Feb</asp:ListItem>
<asp:ListItem Value="3">Mar</asp:ListItem>
<asp:ListItem Value="4">Apr</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator
ID="rfvYear"
runat="server"
ErrorMessage="Please
select year" InitialValue="0" ValidationGroup="vgMonthYear" Display="None" ControlToValidate="ddlYear"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator
ID="rfvMonth"
runat="server"
ValidationGroup="vgMonthYear"
InitialValue="0"
ErrorMessage="Please
select month" Display="None" ControlToValidate="ddlMonth"></asp:RequiredFieldValidator>
<asp:Button ID="btnViewTimeEntry"
runat="server"
Text="View
Entry" ValidationGroup="vgMonthYear" OnClick="ViewEntry" />
<asp:ValidationSummary ID="vsMonthYear"
runat="server"
ValidationGroup="vgMonthYear"
ShowMessageBox="true"
ShowSummary="false"
/>
<fieldset style="width:550px;">
<legend>View Time Entry</legend>
<asp:DataList ID="dlCalendar"
runat="server"
RepeatColumns="4"
RepeatDirection="Horizontal"
onitemdatabound="dlCalendar_ItemDataBound">
<ItemTemplate>
<div style="width:120px; height:80px; border:solid 1px
gray;">
<div style="width:55px; float:left;" id="divDay" runat="server"><%#Eval("Day")
%></div><div style="width:50px; float:right;" id="divDate"
runat="server"><%#Eval("Date")
%></div><div style="float:right;"><asp:CheckBox ID="chkMark" runat="server" /></div>
</div>
</ItemTemplate>
</asp:DataList>
</fieldset>
<div><asp:Button ID="btnSubmit"
runat="server"
Text="SUBMIT"
OnClick="SaveTimeSheet"
/> </div>
<div><asp:Label ID="lblMessage"
runat="server"></asp:Label></div>
</div>
</form>
</body>
</html>
.aspx.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
public partial class TimeSheet :
System.Web.UI.Page
{
DataTable dtCalendar = new
DataTable();
int toDays = DateTime.Now.Day;
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
SqlConnection dbCon = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void
ViewEntry(object sender, EventArgs e)
{
int year = Convert.ToInt32(ddlYear.SelectedValue);
int month = Convert.ToInt32(ddlMonth.SelectedValue);
BindCalendar(year,month);
GetFilledEntries();
}
private void
BindCalendar(int year, int
month)
{
// int year = DateTime.Now.Year;
// int month =
DateTime.Now.Month;
int totalDays = DateTime.DaysInMonth(year,
month);
int toDays = DateTime.Now.Day;
DateTime dt = new DateTime();
DataTable dtCalendar=null;
for (int i = 1; i
<= totalDays; i++)
{
dt = new DateTime(year,
month, i);
string Day = dt.Date.DayOfWeek.ToString();
dtCalendar = CreateDataTable(Day, i);
}
dlCalendar.DataSource = dtCalendar;
dlCalendar.DataBind();
}
private DataTable
CreateDataTable(string day, int date)
{
if (dtCalendar.Rows.Count == 0)
{
dtCalendar = new DataTable();
dtCalendar.Columns.Add("Day",
typeof(string));
dtCalendar.Columns.Add("Date",
typeof(int));
ViewState["dtCalendar"] =
dtCalendar;
}
else
{
dtCalendar = (DataTable)ViewState["dtCalendar"];
}
DataRow drCalendar = dtCalendar.NewRow();
drCalendar["Day"] = day;
drCalendar["Date"] = date;
dtCalendar.Rows.Add(drCalendar);
return dtCalendar;
}
protected void
dlCalendar_ItemDataBound(object sender, DataListItemEventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl
currDay = (HtmlGenericControl)e.Item.FindControl("divDay");
System.Web.UI.HtmlControls.HtmlGenericControl
dateEntry = (HtmlGenericControl)e.Item.FindControl("divDate");
CheckBox chkMark = (CheckBox)e.Item.FindControl("chkMark");
if (Convert.ToUInt32(dateEntry.InnerText)
> toDays || (Convert.ToInt32(ddlYear.SelectedValue)
!= year || Convert.ToInt32(ddlMonth.SelectedValue)
!= month))
{
chkMark.Enabled = false;
}
if (currDay.InnerText.ToLower() == "saturday" ||
currDay.InnerText.ToLower() == "sunday")
{
currDay.Style.Add("background",
"#DEDEDE");
currDay.Style.Add("color", "red");
chkMark.Enabled = false;
}
}
protected void
SaveTimeSheet(object sender, EventArgs e)
{
bool selected = false;
SqlCommand cmd = new
SqlCommand();
foreach (DataListItem
li in dlCalendar.Items)
{
CheckBox chkMark = (CheckBox)li.FindControl("chkMark");
System.Web.UI.HtmlControls.HtmlGenericControl
currDay = (HtmlGenericControl)li.FindControl("divDay");
System.Web.UI.HtmlControls.HtmlGenericControl
dateEntry = (HtmlGenericControl)li.FindControl("divDate");
if (chkMark.Checked == true
&& chkMark.Enabled==true)
{
string submitEntry = "INSERT INTO CalendarEntry(userId, calendarDay,
calendarDate) VALUES (@userID, @calendarDay, @calendarDate)";
cmd = new SqlCommand(submitEntry,
dbCon);
cmd.Parameters.AddWithValue("@userID",
1);
cmd.Parameters.AddWithValue("@calendarDay",
currDay.InnerText);
cmd.Parameters.AddWithValue("@calendarDate",
dateEntry.InnerText);
dbCon.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
dbCon.Close();
chkMark.Checked = true;
chkMark.Enabled = false;
selected = true;
}
}
if (selected == true)
{
lblMessage.Text = "Entry submitted
successfully!";
lblMessage.ForeColor = System.Drawing.Color.Green;
}
else
{
lblMessage.Text = "Please select date to
submit entry!";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
protected void
GetFilledEntries()
{
for (int j = 0; j
< dlCalendar.Items.Count; j++)
{
System.Web.UI.HtmlControls.HtmlGenericControl
currDay = (HtmlGenericControl)dlCalendar.Items[j].FindControl("divDay");
System.Web.UI.HtmlControls.HtmlGenericControl
dateEntry = (HtmlGenericControl)dlCalendar.Items[j].FindControl("divDate");
CheckBox chkMark = (CheckBox)dlCalendar.Items[j].FindControl("chkMark");
string entrySubmitedQuery = "SELECT
calendarDate FROM CalendarEntry WHERE calendarDate=" + Convert.ToInt32(dateEntry.InnerText) + " AND YEAR(submitDate)=" +
ddlYear.SelectedValue + " AND
Month(submitDate)=" + ddlMonth.SelectedIndex + " AND userId=1";
SqlCommand cmdSubmittedQery = new SqlCommand(entrySubmitedQuery,
dbCon);
dbCon.Open();
SqlDataAdapter da = new
SqlDataAdapter(cmdSubmittedQery);
DataTable dtGetResult = new
DataTable();
da.Fill(dtGetResult);
if (dtGetResult.Rows.Count > 0)
{
DataRow drGetResult =
dtGetResult.Rows[0];
if (Convert.ToInt32(dateEntry.InnerText)
== Convert.ToInt32(drGetResult["calendarDate"]))
{
chkMark.Enabled = false;
chkMark.Checked = true;
chkMark.ToolTip = "Entry is already
submitted!";
}
}
dbCon.Close();
}
}
}
SQL Table Script:
Here I have created Table to save time entry, please set
submitDate as Default Date
Create Table CalendarEntry(calId int identity(1,1), userId int, calendarDay varchar(20), calendarDate int, submitDate date)
Now its done, enjoy coding…
Comments