Hi Allen,
I have created sample that fullfill your requirement.
HTML
<div>
<center>
Students Attendance</center>
</div>
<br />
<table>
<tr>
<td>
Box Date
</td>
<td>
<asp:TextBox ID="TxtBoxDate" runat="server" Height="20px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<strong>Hour:</strong>
</td>
<td>
<asp:DropDownList ID="DropDownList5" runat="server" Width="180px">
<asp:ListItem Selected="True">Select Hour</asp:ListItem>
<asp:ListItem>I</asp:ListItem>
<asp:ListItem>II</asp:ListItem>
<asp:ListItem>III</asp:ListItem>
<asp:ListItem>IV</asp:ListItem>
<asp:ListItem>V</asp:ListItem>
<asp:ListItem>VI</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<strong>Day:</strong>
</td>
<td>
<asp:TextBox ID="TxtBoxDay" runat="server" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="REGNO" HeaderText="REGNO" />
<asp:BoundField DataField="FIRSTNAME" HeaderText="FIRST NAME" />
<asp:BoundField DataField="LASTNAME" HeaderText="LAST NAME" />
<asp:TemplateField HeaderText="PRESENT/ABSENT">
<ItemTemplate>
<asp:CheckBox ID="cbStatus" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AUTHORIZED ABSENT">
<ItemTemplate>
<asp:CheckBox ID="cbAuthorized" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td class="auto-style7" colspan="2">
<asp:Button ID="BtnSubmit" Text="Submit" runat="server" OnClick="Button2_Click" />
</td>
</tr>
</table>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("regNo", typeof(int)),
new DataColumn("FirstName", typeof(string)),
new DataColumn("LastName",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
int regNo = Convert.ToInt32(row.Cells[0].Text.Trim());
string firstName = row.Cells[1].Text.Trim();
string lastName = row.Cells[2].Text.Trim();
string boxDay = TxtBoxDay.Text.Trim();
DateTime boxDate = Convert.ToDateTime(TxtBoxDate.Text.Trim());
string hours = DropDownList5.SelectedItem.Text.Trim();
string status = string.Empty;
string authorized = string.Empty;
CheckBox cbStatus = (CheckBox)row.FindControl("cbStatus");
if (cbStatus.Checked)
{
status = "A";
}
else
{
status = "P";
}
CheckBox cbAuthorized = (CheckBox)row.FindControl("cbAuthorized");
if (cbAuthorized.Checked)
{
authorized = "O";
}
else
{
authorized = string.Empty;
}
InsertAttendance(regNo, firstName, lastName, boxDay, boxDate, hours, status, authorized);
}
Response.Write("Record Added Successfully!");
}
private void InsertAttendance(int regNo, string firstName, string lastName, string boxDay, DateTime boxDate, string hours, string status, string authorized)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "INSERT INTO AttendanceTable VALUES (@RegNo,@FirstName,@LastName,@BoxDay,@BoxDate,@Hours,@Status,@Authorized)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@RegNo", regNo);
cmd.Parameters.AddWithValue("@FirstName", firstName);
cmd.Parameters.AddWithValue("@LastName", lastName);
cmd.Parameters.AddWithValue("@BoxDay", boxDay);
cmd.Parameters.AddWithValue("@BoxDate", boxDate);
cmd.Parameters.AddWithValue("@Hours", hours);
cmd.Parameters.AddWithValue("@Status", status);
cmd.Parameters.AddWithValue("@Authorized", authorized);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Screenshot
After Inserted into Databse.