Attendance system in asp.net c#
In this article, we learn how to mark attendance and we will try to learn how to save your attendance in database in asp.net c# .Here we bind the whole dynamically months values in gridview and only chance to mark same day attendance and save the values in database .but this article you need put validation according to your requirement.
Database script
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Attendance](
[ID] [int] IDENTITY(1,1) NOT NULL,
[stuid] [int] NULL,
[attdate] [datetime] NULL,
[attmonth] [int] NULL,
[attyear] [int] NULL,
[attStatus] [bit] NULL,
[remarks] [varchar](max) NULL,
[loggedInDate] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
Source code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="studentattendance.aspx.cs" Inherits="studentattendance" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Attendance system in asp.net C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gdattendance" HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" runat="server" ShowHeader="False" AutoGenerateColumns="False" OnRowDataBound="gdattendance_RowDataBound" EnableModelValidation="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<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" Placeholder="Remarks" Font-Size="8" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkmark" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" BackColor="#FFF7E7" ForeColor="#8C4510" />
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle HorizontalAlign="Center" BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:Button ID="btnattendance" runat="server" Text="Mark your attendance" OnClick="btnattendance_Click" />
</div>
</form>
</body>
</html>
Code behind(c#)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class studentattendance : System.Web.UI.Page
{
string CurrntMonthYear = "";
SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
int Year = 0;
int Month = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindAttendance();
}
}
protected void bindAttendance()
{
// current Year
Year = DateTime.Now.Year;
// current Month
Month = 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, Month, i); // find days name
strDate = new DateTime(Year, Month, 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
}
// Current Date, Month and Year
CurrntMonthYear = DateTime.Now.ToString("dd") + " " + DateTime.Now.ToString("MMMM") + " " + Year;
// DataTable to GridView
gdattendance.DataSource = Dt;
gdattendance.DataBind();
}
protected void gdattendance_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)
{
chk.Enabled = false;
txtRemark.Enabled = false;
}
if ((Convert.ToDateTime(rowDate) > Convert.ToDateTime(currDate)) || chk.Checked == true)
{
chk.Enabled = false;
txtRemark.Enabled = false;
}
// we make saturday and sunday with red forecolor
if (rowDay.Equals("Saturday") || rowDay.Equals("Sunday"))
{
e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
chk.Enabled = false;
txtRemark.Enabled = false;
}
}
}
protected void SaveData(int attStatus, string strRemarks, string strDate)
{
//let's assume that are student id is 1
string strQry = "INSERT INTO Attendance (stuid, attmonth, attyear, attStatus, remarks, attdate, loggedInDate ) VALUES (1," + DateTime.Now.Month + "," + DateTime.Now.Year + "," + attStatus + ", '" + strRemarks + "', '" + strDate + "',getDate())";
SqlCommand cmd = new SqlCommand(strQry, con);
con.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
protected void bindPrevAtt(out bool attStatus, out string strRemarks, string strAttDate)
{
attStatus = false;
strRemarks = "Remarks";
string strQry = "SELECT attStatus, remarks FROM Attendance WHERE stuid = 1 AND Convert(varchar(12),attDate,103) = '" + strAttDate + "'";
SqlCommand cmd = new SqlCommand(strQry, con);
con.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();
con.Close();
}
protected void btnattendance_Click(object sender, EventArgs e)
{
string strRemarks = "";
string currenthour = DateTime.Now.Hour.ToString();
string currentmin = DateTime.Now.Minute.ToString();
foreach (GridViewRow gdrow in gdattendance.Rows)
{
string strDay = gdrow.Cells[1].Text; //Day
string strDate = gdrow.Cells[2].Text; //Date
TextBox txtremarks = (TextBox)gdrow.FindControl("txtremarks");
CheckBox chkmark = (CheckBox)gdrow.FindControl("chkmark");
if (chkmark.Checked == true)
{
if (Convert.ToInt32(currenthour) > 10 || Convert.ToInt32(currentmin) > 60)
{
strRemarks = "Sorry you are late you have marked your attendance on " + currenthour + ":" + currentmin + "";
}
else
{
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();
Response.Write("<script>alert('Thank you for mark your attendance')</script>");
}
}
Out-Put:-
Attendance system in asp.net c#
Reviewed by NEERAJ SRIVASTAVA
on
11:49:00 PM
Rating:
Hi friend your article is nice but one major problem,there is n hits on server.avoid this
ReplyDelete