Sei sulla pagina 1di 5

Employee Attendance System module using ASP.

NET (C#)
Home.aspx <%@ Page Language="C#" AutoEventWireup="true"CodeFile="home.aspx.cs" Inherits ="home" %> 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>Attendance Systemtitle> head> <body> <form id="form1" runat="server"> <div style="text-align:center;"> <div style="font-size:medium; color:Blue; text-align:center;">Todays Date : <%=strCurrntMonthYear %>div> <asp:GridView ID="gvCalander" Font-Size="Smaller" Font-Names="verdana, arial" HeaderStyle-HorizontalAlign="Center"RowStyleHorizontalAlign="Center" RowStyle-BackColor="gray"AlternatingRowStyleBackColor="Aqua" CellPadding="5" CellSpacing="5" runat="server" ShowHeader="f alse"AutoGenerateColumns="false"OnRowDataBound="gvCalander_RowDataBound"> <Columns> <asp:BoundField DataField="AutoID" HeaderText="Days" /> <asp:BoundField DataField="DaysName" HeaderText="Name" /> <asp:BoundField DataField="Date" HeaderText="Name" /> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="txtRemarks" runat="server" Text="Remarks"FontSize="8" onfocus="if(this.value=='Remarks'){this.value=''}"onblur="if(this.va lue==''){this.value='Remarks'}" >asp:TextBox> ItemTemplate> asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkMark" runat="server" /> ItemTemplate> asp:TemplateField> Columns> <RowStyle BackColor="Gray" HorizontalAlign="Center" /> <HeaderStyle HorizontalAlign="Center" /> <AlternatingRowStyle BackColor="Aqua" /> asp:GridView> div> <div style="text-align:center;"> <asp:Button ID="btnAddAttendence" runat="server" Text="Add"OnClick="btnAd dAttendence_Click" /> <asp:Button ID="btnReset"runat="server" Text="Reset" /> div> form> body> html> Home.aspx.cs Page using System;

using using using using using using using using using using using

System.Data; System.Configuration; System.Collections; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.Text; System.Data.SqlClient;

public partial class home : System.Web.UI.Page { public static string strCurrntMonthYear = ""; SqlConnection dbCon = newSqlConnection(System.Configuration.ConfigurationManager.AppSettings["con nString"].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)) || chk.Checked==true) { // CheckBox chk = (CheckBox)e.Row.FindControl("chkMark"); // TextBox txtRemark = (TextBox)e.Row.FindControl("txtRemarks"); chk.Enabled = false; txtRemark.Enabled = false; } 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 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 == true) { 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 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(); } protected void bindPrevAtt(out bool attStatus, out stringstrRemarks, stri ng 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(); } } Table Scripts /****** Object: Table [dbo].[AttendanceMaster] 12:38:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[AttendanceMaster1]( [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 GO DEFAULT ((0)) FOR[attStatus] Script Date: 11/21/2011

Potrebbero piacerti anche