Hello Everyone,
Thank you all for your kind support and feedback. I am very glad to all my readers who are regularly reading my blogs and updating me with their valuable feedback..
I received many more feedback and personal mails regarding
Thank you all for your kind support and feedback. I am very glad to all my readers who are regularly reading my blogs and updating me with their valuable feedback..
I received many more feedback and personal mails regarding
Here on suggestion and feedback I am giving you the updated version of the same system. You can enjoy the latest version and let me know in case if you need any help..
--Here is HTML CODE
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Attendance System</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center;">
<div style="font-size:20px; color:#4d0039; text-align:center;margin-bottom:10px;"><strong>Employee
Attendance System</strong></div>
<div style="font-size:medium; color:#4d0039; text-align:center;margin-bottom:10px;"><strong> Today is
<%=strCurrntMonthYear %></strong></div>
<div style="font-size:medium; color:#4d0039; text-align:center;margin-bottom:10px;"
id="divMsg"
runat="server"></div>
<asp:GridView ID="gvCalander" Width="80%" Font-Size="Smaller" Font-Names="verdana, arial" HeaderStyle-HorizontalAlign="Center" ShowHeader="true" HeaderStyle-BackColor="#ffb3b3" RowStyle-HorizontalAlign="Center" RowStyle-BackColor="gray" AlternatingRowStyle-BackColor="#ffe6f8" CellPadding="5"
CellSpacing="5"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="gvCalander_RowDataBound">
<Columns>
<asp:BoundField DataField="AutoID"
HeaderText="Days#"
ControlStyle-Width="20"
/>
<asp:BoundField DataField="DaysName"
HeaderText="Day"
HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ControlStyle-Width="50"
/>
<asp:BoundField DataField="Date"
HeaderText="Date"
HeaderStyle-HorizontalAlign="Center" ControlStyle-Width="50"
/>
<asp:TemplateField HeaderText="Remarks"
ControlStyle-Width="200"
>
<ItemTemplate>
<asp:TextBox ID="txtRemarks"
Enabled="false"
runat="server"
Width="200"
Text="Remarks"
Font-Size="8"
onfocus="if(this.value=='Remarks'){this.value=''}"
onblur="if(this.value==''){this.value='Remarks'}"
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Attendance">
<ItemTemplate>
<asp:CheckBox ID="chkMark"
Enabled="false"
runat="server"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#ffe6f8"
HorizontalAlign="Center"
/>
<HeaderStyle HorizontalAlign="Center"
/>
<AlternatingRowStyle
BackColor="#f7e6ff"
/>
</asp:GridView>
</div>
<div style="text-align:center; margin-top:10px;">
<asp:Button ID="btnAddAttendence"
runat="server"
Text="Add"
OnClick="btnAddAttendence_Click"
/> <asp:Button ID="btnReset" runat="server" Text="Reset" />
</div>
</form>
</body>
</html>
--Here is C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
public static string strCurrntMonthYear = "";
SqlConnection dbCon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"].ToString());
int Year = 0;
int inMonth = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindAttendance();
}
}
protected void bindAttendance()
{
//get current Year
Year = DateTime.Now.Year;
//get current Month
inMonth = DateTime.Now.Month;
//get Day's in current month
int Days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
//Declare DataTable
DataTable Dt = new DataTable("dtDays");
//Declare Data Column
DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
Dt.Columns.Add(auto);
DataColumn DaysName = new DataColumn("DaysName", typeof(string));
Dt.Columns.Add(DaysName);
DataColumn Date = new DataColumn("Date", typeof(string));
Dt.Columns.Add(Date);
//Declare Data Row
DataRow dr = null;
DateTime days;
DateTime strDate;
for (int i = 1; i <= Days; i++)
{
//Create row in DataTable
dr = Dt.NewRow();
days = new DateTime(Year, inMonth, i); // find days name
strDate = new DateTime(Year, inMonth, i); // find date w.r.t days
dr["AutoID"] = i;
dr["DaysName"] = days.DayOfWeek;
dr["Date"] = strDate.Date.ToShortDateString();
Dt.Rows.Add(dr); //Add row in DataTable
}
//Assign Current Date, Month and Year
strCurrntMonthYear = DateTime.Now.ToString("dd") + " " + DateTime.Now.ToString("MMMM") + " " + Year;
//Assing DataTable to GridView
gvCalander.DataSource = Dt;
gvCalander.DataBind();
}
protected void gvCalander_RowDataBound(object sender, GridViewRowEventArgs e)
{
string currDate = DateTime.Now.ToShortDateString();
if (e.Row.RowType == DataControlRowType.DataRow)
{
string rowDate = e.Row.Cells[2].Text; //Date
string rowDay = e.Row.Cells[1].Text; //Day
CheckBox chk = (CheckBox)e.Row.FindControl("chkMark");
TextBox txtRemark = (TextBox)e.Row.FindControl("txtRemarks");
string strRemarks = "";
bool boolAttStatus = false;
bindPrevAtt(out boolAttStatus, out strRemarks, rowDate);
txtRemark.Text = strRemarks;
chk.Checked = boolAttStatus;
if ((Convert.ToDateTime(rowDate) == Convert.ToDateTime(currDate)))
{
// CheckBox chk = (CheckBox)e.Row.FindControl("chkMark");
// TextBox txtRemark = (TextBox)e.Row.FindControl("txtRemarks");
bool visibility = chk.Checked == true ? false : true;
chk.Enabled = visibility;
txtRemark.Enabled = visibility;
}
if (rowDay.Equals("Sunday") || rowDay.Equals("Saturday")) //if there is Sunday make it red colour
{
e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
}
}
}
protected void btnAddAttendence_Click(object sender, EventArgs e)
{
string strRemarks = "";
string tsCurrHour = DateTime.Now.Hour.ToString();
string tsCurrMin = DateTime.Now.Minute.ToString();
foreach (GridViewRow gvr in gvCalander.Rows)
{
string currDate = DateTime.Now.ToShortDateString();
string strDay = gvr.Cells[1].Text; //Day
string strDate = gvr.Cells[2].Text; //Date
TextBox txtRemarks = (TextBox)gvr.FindControl("txtRemarks");
CheckBox chkMark = (CheckBox)gvr.FindControl("chkMark");
if (chkMark.Checked == false && currDate == strDate)
{
divMsg.Style.Add("color", "red");
divMsg.InnerHtml = "Please mark attendance first.";
return;
}
else if (chkMark.Checked == true && currDate == strDate)
{
if (Convert.ToInt32(tsCurrHour) > 10 || Convert.ToInt32(tsCurrMin) > 30)
{
strRemarks = "Sorry you are late";
}
else
{
strRemarks = txtRemarks.Text.Trim();
}
//strRemarks = txtRemarks.Text.Trim();
//Save Data
DateTime dt = Convert.ToDateTime(strDate);
string strDateTime = dt.Month + "/" + dt.Day + "/" + dt.Year;
SaveData(1, strRemarks, strDateTime);
}
}
//bind Attendance
bindAttendance();
}
protected void SaveData(int attStatus, string strRemarks, string strDate)
{
//here I am assuming logged in employee Id as 1
try
{
string strQry = "INSERT INTO AttendanceMaster (empId, attMonth, attYear, attStatus, remarks, attdate, loggedInDate ) VALUES (1," + DateTime.Now.Month + "," + DateTime.Now.Year + "," + attStatus + ", '" + strRemarks + "', '" + strDate + "',getDate())";
SqlCommand cmd = new SqlCommand(strQry, dbCon);
dbCon.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
dbCon.Close();
divMsg.Style.Add("color", "green");
divMsg.InnerHtml = "Attendance logged in system.";
}
catch (Exception ex)
{
divMsg.InnerHtml = "Error occured while adding attendance in system";
}
}
protected void bindPrevAtt(out bool attStatus, out string strRemarks, string strAttDate)
{
attStatus = false;
strRemarks = "Remarks";
string strQry = "SELECT attStatus, remarks FROM AttendanceMaster WHERE empId = 1 AND Convert(varchar(12),attDate,103) = '" + strAttDate + "'";
SqlCommand cmd = new SqlCommand(strQry, dbCon);
dbCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
strRemarks = dt.Rows[0]["remarks"].ToString();
attStatus = Convert.ToBoolean(dt.Rows[0]["attStatus"]);
}
dt.Dispose();
da.Dispose();
cmd.Dispose();
dbCon.Close();
}
}
--Here is Table Scripts
CREATE TABLE [dbo].[AttendanceMaster](
[attID] [int] IDENTITY(1,1) NOT NULL,
[empId] [int] NULL,
[attmonth] [int] NULL,
[attYear] [int] NULL,
[attStatus] [bit] NULL,
[remarks] [varchar](200) NULL,
[attdate] [datetime] NULL,
[loggedInDate] [datetime] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[AttendanceMaster] ADD DEFAULT ((0)) FOR [attStatus]
Please check the latest code and send your feedback on the same.
Enjoy Coding..
Regards,
ved pathak
--Here is HTML CODE
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Attendance System</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center;">
<div style="font-size:20px; color:#4d0039; text-align:center;margin-bottom:10px;"><strong>Employee
Attendance System</strong></div>
<div style="font-size:medium; color:#4d0039; text-align:center;margin-bottom:10px;"><strong> Today is
<%=strCurrntMonthYear %></strong></div>
<div style="font-size:medium; color:#4d0039; text-align:center;margin-bottom:10px;"
id="divMsg"
runat="server"></div>
<asp:GridView ID="gvCalander" Width="80%" Font-Size="Smaller" Font-Names="verdana, arial" HeaderStyle-HorizontalAlign="Center" ShowHeader="true" HeaderStyle-BackColor="#ffb3b3" RowStyle-HorizontalAlign="Center" RowStyle-BackColor="gray" AlternatingRowStyle-BackColor="#ffe6f8" CellPadding="5"
CellSpacing="5"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="gvCalander_RowDataBound">
<Columns>
<asp:BoundField DataField="AutoID"
HeaderText="Days#"
ControlStyle-Width="20"
/>
<asp:BoundField DataField="DaysName"
HeaderText="Day"
HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ControlStyle-Width="50"
/>
<asp:BoundField DataField="Date"
HeaderText="Date"
HeaderStyle-HorizontalAlign="Center" ControlStyle-Width="50"
/>
<asp:TemplateField HeaderText="Remarks"
ControlStyle-Width="200"
>
<ItemTemplate>
<asp:TextBox ID="txtRemarks"
Enabled="false"
runat="server"
Width="200"
Text="Remarks"
Font-Size="8"
onfocus="if(this.value=='Remarks'){this.value=''}"
onblur="if(this.value==''){this.value='Remarks'}"
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Attendance">
<ItemTemplate>
<asp:CheckBox ID="chkMark"
Enabled="false"
runat="server"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#ffe6f8"
HorizontalAlign="Center"
/>
<HeaderStyle HorizontalAlign="Center"
/>
<AlternatingRowStyle
BackColor="#f7e6ff"
/>
</asp:GridView>
</div>
<div style="text-align:center; margin-top:10px;">
<asp:Button ID="btnAddAttendence"
runat="server"
Text="Add"
OnClick="btnAddAttendence_Click"
/> <asp:Button ID="btnReset" runat="server" Text="Reset" />
</div>
</form>
</body>
</html>
--Here is C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
public static string strCurrntMonthYear = "";
SqlConnection dbCon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"].ToString());
int Year = 0;
int inMonth = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindAttendance();
}
}
protected void bindAttendance()
{
//get current Year
Year = DateTime.Now.Year;
//get current Month
inMonth = DateTime.Now.Month;
//get Day's in current month
int Days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
//Declare DataTable
DataTable Dt = new DataTable("dtDays");
//Declare Data Column
DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
Dt.Columns.Add(auto);
DataColumn DaysName = new DataColumn("DaysName", typeof(string));
Dt.Columns.Add(DaysName);
DataColumn Date = new DataColumn("Date", typeof(string));
Dt.Columns.Add(Date);
//Declare Data Row
DataRow dr = null;
DateTime days;
DateTime strDate;
for (int i = 1; i <= Days; i++)
{
//Create row in DataTable
dr = Dt.NewRow();
days = new DateTime(Year, inMonth, i); // find days name
strDate = new DateTime(Year, inMonth, i); // find date w.r.t days
dr["AutoID"] = i;
dr["DaysName"] = days.DayOfWeek;
dr["Date"] = strDate.Date.ToShortDateString();
Dt.Rows.Add(dr); //Add row in DataTable
}
//Assign Current Date, Month and Year
strCurrntMonthYear = DateTime.Now.ToString("dd") + " " + DateTime.Now.ToString("MMMM") + " " + Year;
//Assing DataTable to GridView
gvCalander.DataSource = Dt;
gvCalander.DataBind();
}
protected void gvCalander_RowDataBound(object sender, GridViewRowEventArgs e)
{
string currDate = DateTime.Now.ToShortDateString();
if (e.Row.RowType == DataControlRowType.DataRow)
{
string rowDate = e.Row.Cells[2].Text; //Date
string rowDay = e.Row.Cells[1].Text; //Day
CheckBox chk = (CheckBox)e.Row.FindControl("chkMark");
TextBox txtRemark = (TextBox)e.Row.FindControl("txtRemarks");
string strRemarks = "";
bool boolAttStatus = false;
bindPrevAtt(out boolAttStatus, out strRemarks, rowDate);
txtRemark.Text = strRemarks;
chk.Checked = boolAttStatus;
if ((Convert.ToDateTime(rowDate) == Convert.ToDateTime(currDate)))
{
// CheckBox chk = (CheckBox)e.Row.FindControl("chkMark");
// TextBox txtRemark = (TextBox)e.Row.FindControl("txtRemarks");
bool visibility = chk.Checked == true ? false : true;
chk.Enabled = visibility;
txtRemark.Enabled = visibility;
}
if (rowDay.Equals("Sunday") || rowDay.Equals("Saturday")) //if there is Sunday make it red colour
{
e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
}
}
}
protected void btnAddAttendence_Click(object sender, EventArgs e)
{
string strRemarks = "";
string tsCurrHour = DateTime.Now.Hour.ToString();
string tsCurrMin = DateTime.Now.Minute.ToString();
foreach (GridViewRow gvr in gvCalander.Rows)
{
string currDate = DateTime.Now.ToShortDateString();
string strDay = gvr.Cells[1].Text; //Day
string strDate = gvr.Cells[2].Text; //Date
TextBox txtRemarks = (TextBox)gvr.FindControl("txtRemarks");
CheckBox chkMark = (CheckBox)gvr.FindControl("chkMark");
if (chkMark.Checked == false && currDate == strDate)
{
divMsg.Style.Add("color", "red");
divMsg.InnerHtml = "Please mark attendance first.";
return;
}
else if (chkMark.Checked == true && currDate == strDate)
{
if (Convert.ToInt32(tsCurrHour) > 10 || Convert.ToInt32(tsCurrMin) > 30)
{
strRemarks = "Sorry you are late";
}
else
{
strRemarks = txtRemarks.Text.Trim();
}
//strRemarks = txtRemarks.Text.Trim();
//Save Data
DateTime dt = Convert.ToDateTime(strDate);
string strDateTime = dt.Month + "/" + dt.Day + "/" + dt.Year;
SaveData(1, strRemarks, strDateTime);
}
}
//bind Attendance
bindAttendance();
}
protected void SaveData(int attStatus, string strRemarks, string strDate)
{
//here I am assuming logged in employee Id as 1
try
{
string strQry = "INSERT INTO AttendanceMaster (empId, attMonth, attYear, attStatus, remarks, attdate, loggedInDate ) VALUES (1," + DateTime.Now.Month + "," + DateTime.Now.Year + "," + attStatus + ", '" + strRemarks + "', '" + strDate + "',getDate())";
SqlCommand cmd = new SqlCommand(strQry, dbCon);
dbCon.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
dbCon.Close();
divMsg.Style.Add("color", "green");
divMsg.InnerHtml = "Attendance logged in system.";
}
catch (Exception ex)
{
divMsg.InnerHtml = "Error occured while adding attendance in system";
}
}
protected void bindPrevAtt(out bool attStatus, out string strRemarks, string strAttDate)
{
attStatus = false;
strRemarks = "Remarks";
string strQry = "SELECT attStatus, remarks FROM AttendanceMaster WHERE empId = 1 AND Convert(varchar(12),attDate,103) = '" + strAttDate + "'";
SqlCommand cmd = new SqlCommand(strQry, dbCon);
dbCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
strRemarks = dt.Rows[0]["remarks"].ToString();
attStatus = Convert.ToBoolean(dt.Rows[0]["attStatus"]);
}
dt.Dispose();
da.Dispose();
cmd.Dispose();
dbCon.Close();
}
}
--Here is Table Scripts
CREATE TABLE [dbo].[AttendanceMaster](
[attID] [int] IDENTITY(1,1) NOT NULL,
[empId] [int] NULL,
[attmonth] [int] NULL,
[attYear] [int] NULL,
[attStatus] [bit] NULL,
[remarks] [varchar](200) NULL,
[attdate] [datetime] NULL,
[loggedInDate] [datetime] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[AttendanceMaster] ADD DEFAULT ((0)) FOR [attStatus]
Please check the latest code and send your feedback on the same.
Enjoy Coding..
Regards,
ved pathak
Comments
Please tell me in brief what exactly your requirement is. Or Send me your code on vedrajan@gmail.com
Regards,
ved pathak
It's my pleasure to help who need help, I do not hide anything to help..
You can simply apply the code in your web page and it will work automatically. Read and understand full code properly if you need any help, please let me know.
Thanks.