Try this
Html Code
<div style="margin-left: 40px">
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkAttendance" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Id">
<ItemTemplate>
<asp:Label ID="lblStudentId" runat="server" Text='<%#Eval("Std_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Roll No">
<ItemTemplate>
<asp:Label ID="lblStudentRollNo" runat="server" Text='<%#Eval("RollNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblStudentName" runat="server" Text='<%#Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Type">
<ItemTemplate>
<asp:Label ID="lblStudentCourseType" runat="server" Text='<%#Eval("Course_Type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lecture Type">
<ItemTemplate>
<asp:TextBox ID="txtLectureType" runat="server" Text='<%#Eval("Lecture_Type") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvLectureType" runat="server" ControlToValidate="txtLectureType"
ErrorMessage="*" ValidationGroup="AttendenceGroup"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
Date of Attendence:
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox><span> Date Format: yyyy-MM-dd</span>
<br />
Lecture Type:<asp:TextBox ID="txtLectureType" runat="server"
style="z-index: 1; left: 37px; top: 9px; position: relative"></asp:TextBox>
<br />
<asp:Button ID="btnSaveAttendences" runat="server" Text="Save Attendence" OnClick="SaveStudentAttendances"
ValidationGroup="AttendenceGroup"
style="z-index: 1; left: 327px; top: 476px; position: absolute" />
</div>
Filter By Course Type:<asp:DropDownList ID="ddlCourseType" runat="server"
style="z-index: 1; left: 657px; top: 472px; position: absolute">
</asp:DropDownList>
<asp:Button ID="btnFilterGridView" runat="server" Text="Filter Grid"
OnClick="btnFilterGridView_Click"
style="z-index: 1; left: 803px; top: 472px; position: absolute" />
Code
Namespaces
using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.gvStudents.Enabled = true;
this.gvStudents.DataSource = this.GetData();
this.gvStudents.DataBind();
this.PopulateDropDownList();
}
}
private DataTable GetData()
{
string connection = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table_Attendance", con))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
}
private void PopulateDropDownList()
{
this.ddlCourseType.AppendDataBoundItems = true;
this.ddlCourseType.Items.Add(new ListItem { Text = "Please Select", Value = string.Empty });
this.ddlCourseType.DataSource = this.GetData();
this.ddlCourseType.DataTextField = "Course_Type";
this.ddlCourseType.DataValueField = "Course_Type";
this.ddlCourseType.DataBind();
}
protected void btnFilterGridView_Click(object sender, EventArgs e)
{
this.FilterAttendenceGrid();
}
private void FilterAttendenceGrid()
{
if (this.ddlCourseType.SelectedIndex != 0)
{
string connection = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table_Attendance WHERE Course_Type=@CourseType", con))
{
cmd.Parameters.AddWithValue("@CourseType", this.ddlCourseType.SelectedItem.Value);
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
this.gvStudents.DataSource = dt;
this.gvStudents.DataBind();
}
}
}
}
}
protected void SaveStudentAttendances(object sender, EventArgs e)
{
bool IsAvailable = false;
foreach (GridViewRow row in gvStudents.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string attendanceStatus = string.Empty;
CheckBox chkAttendance = row.FindControl("chkAttendence") as CheckBox;
attendanceStatus = chkAttendance.Checked ? "Present" : "Absent";
int stdId = int.Parse((row.FindControl("lblStudentId") as Label).Text.Trim());
string rollNo = (row.FindControl("lblStudentRollNo") as Label).Text.Trim();
string studentName = ((row.FindControl("lblStudentName") as Label).Text.Trim());
string courseType = (row.FindControl("lblStudentCourseType") as Label).Text.Trim();
string constring = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveAttendence";
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@StudentName", studentName);
cmd.Parameters.AddWithValue("@StudentRollNo", rollNo);
cmd.Parameters.AddWithValue("@AttendenceStatus", attendanceStatus);
cmd.Parameters.AddWithValue("@AttendenceDate", this.txtDate.Text.Trim());
cmd.Parameters.AddWithValue("@LectureType", this.txtLectureType.Text.Trim());
cmd.Parameters.AddWithValue("@CourseType", courseType);
IsAvailable = Convert.ToBoolean(cmd.ExecuteScalar());
row.Enabled = !(IsAvailable);
con.Close();
}
}
}
}
}