Employee Attendance System module using ASP.NET (C#)


Home.aspx

<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:medium; color:Blue; text-align:center;">Today’s Date : <%=strCurrntMonthYear %></div>
    <asp:GridView ID="gvCalander"  Font-Size="Smaller" Font-Names="verdana, arial" HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" RowStyle-BackColor="gray" AlternatingRowStyle-BackColor="Aqua" CellPadding="5"  CellSpacing="5" runat="server" ShowHeader="false" 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" Font-Size="8" onfocus="if(this.value=='Remarks'){this.value=''}" onblur="if(this.value==''){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="btnAddAttendence_Click" /> <asp:Button ID="btnReset" runat="server" Text="Reset" />
    </div>
    </form>
</body>

</html>


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

public partial class home : 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)) || 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 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();
    }

}

Table Scripts
/****** Object:  Table [dbo].[AttendanceMaster]    Script Date: 11/21/2011 12:38:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

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]
GO

Note: This is a module of Employee Management System (Attendance), you can integrate with your existing system using this Attendance System. Yo need to map your existig employee Id with AttendanceMaster Table. 

Comments

Unknown said…
THANX SIR BUT IMPLEMENT KE BAAD ACCHA SA THANX BOLUNGA
VedPathak said…
You are welcome sir,
You implement it and if you face any difficulties please let me know.

Koi baat nahi sir, itna hi kafi hai mere liye.

Regards,
ved
Timelabs said…

Great post..!!!!
I like this Post. It is so nice to read such wonderful blog. Thanks for sharing!

Regards
Time Labs
VedPathak said…
Thanks Mr. Devesh for your appreciation.

I am looking forward more feedback/comments on same.

Thank you once again.
Regards,
ved pathak
Unknown said…
Thanks sir...
Great Post...
VedPathak said…
You are most welcome Neha, It's my pleasure to get comments/feedback from you people to improve my blogging.

Always appreciate your suggestions.
Thanks.
Anonymous said…
sir your .aspx file not configured correctly it have erors a lot
VedPathak said…
Dear Abrar,

Thanks for your suggestion but it is html issues. I tried to post it many times but some tags are always missing while posting.

Please correct after pasting the code in your page. Sorry for Inconvenience.

Regards,
ved
Abel Gebray said…
Dear Ved could u send me by word document without errors
VedPathak said…
Dear Mr. Gebray,

Thanks for your feedback. I am not getting what exactly document you need about. It's very simple step by step explained in blog.

If you face any kind of difficulties, kindly mail me your code so I can send you by resolving it.

Thanks & Regards,
ved pathak
vedrajan@gmail.com
Unknown said…
Hello Sir. May I ask? Where can you declare the gvCalendar in the GridView? Thank you :)
VedPathak said…
Sorry for delayed reply.sure Cruz, you can put delete button and make the delete functionality accordingly (like save). Let me know if you want more clarifications.
thank you sir for this important information.
VedPathak said…
Thank you Mukesh,

for your appreciation!
Unknown said…
Thanks Sir.
Great post.
Sir concerning the html part can you also send it to my mail below?
Am new into programming.
Will really appreciate it if u can send me the html part.

allenteeno@gmail.com
VedPathak said…
Thank you Benjamin Allen for your response. Will try to send you entire but it is easy if you can do it by your own you will get understand code properly.

Simply copy and paste the code in aspx and .cs.

Thanks,
ved pathak
VedPathak said…
Dear All,

We have updated the code, now you are able to copy the aspx part as well without any missing tags.

Thanks for your suggestion.
Cheers,
ved pathak
Unknown said…
Hello please help me out i m facing error in gvCalander.Rows
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);
}
VedPathak said…
Dear Hannan Saifullah,

I am sorry but I am unable to find exactly what kind of error are you facing? The above code is complete please check it thoroughly and let me know the exact error message which you are getting.

Thanks.
HIMANSHU said…
Dear Sir,

I want to make attendance page like this video on youtube (https://www.youtube.com/watch?v=a0LsniFpDPE). Can you help me in this regard.

Himanshu Goel
himanshu3335@gmail.com
VedPathak said…
Dear Himanshu,

Sure, you can do that way also. You just need to write SQL Query to get month wise day and Student details (RollNo, Name). That way you can just check attendance.
You try it and let me know , by the help of this tutorials you can create the same.

Thanks,
ved pathak
Unknown said…
This post is so informative and makes a very nice image on the topic in my mind. It is the first time I visit your blog, but I was extremely impressed. Keep posting as I am gonna come to read it everyday!

Time and Attendance System
VedPathak said…
Thanks Kyile,

Thank you very much for your appreciation.

Surely, will keep posting useful topics which helps you.

Thank you for visiting.
Unknown said…
Dear sir,

I copied your code and tried to debug..it shows error at da.Fill(dt);
Can you tell me,how to insert data.

Thanks
VedPathak said…
Hi Rohit,

Thank your very much for visiting our blog. First of all I would like to know what type of error are you getting?

There could be two reason as per your post:

1. Either you have not declared proper namespace for Data Table, Data Adapter i.e. SQL Data Client OR

2. There might be problem while copying the code.

I suggest you to please write the code instead of copying it and then try to run again and let me know if you are facing the same problem again.

Thanks & Regards,
ved pathak
James Brown said…
Interesting blog and I really like your work and must appreciate you work for the attendance system with SMS well done.
rfid attendance system with SMS
VedPathak said…
Thank you very much for visiting blog and your appreciation Mr. James Brown.

Keep sending your suggestion.

Thanks again.

Ved Pathak

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'ASP.attendancesheet_aspx' does not contain a definition for 'gvCalander_RowDataBound' and no extension method 'gvCalander_RowDataBound' accepting a first argument of type 'ASP.attendancesheet_aspx' could be found (are you missing a using directive or an assembly reference?)
Line 7:
VedPathak said…
hi Unknown,

you have not copied the code properly, anyway for you I have modified the code here you can see updated code

http://vedpathak.blogspot.in/2018/02/employee-attendance-system-modified.html

please let me know
Unknown said…
Thanks for sharing this kind of useful information epunchtime offers best service.
For More Information. Click here
Unknown said…
Thanks for sharing this useful information with us ,i am glad to read this blog.
need for employee managment system
Unknown said…
great blog with good content. thanks for sharing this blog with us. For more Employee Management System Click here.
VedPathak said…
Thanks Dear for your feedback...
Sidra Naz said…
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. zeiterfassung app
VedPathak said…
Sure, I would like to hear your questions/comments and feedback.

Thanks.
Flowace said…
Your blog is awesome with useful stuff. Thanks for sharing it with us. If you want to learn more about Online Attendance Tracker
, please click here.