Hi irshad1231,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Repeater ID="rptStudents" runat="server">
<HeaderTemplate>
<table id="tblStudents" class="table table-bordered" border="1" cellpadding="1"
cellspacing="1" style="border-style: solid; border-width: thin; text-align: center;">
<thead>
<tr>
<th class="thright">StudentID</th>
<th class="thright">ispresnt1 </th>
<th class="thright">ispresent2</th>
<th class="thright">absent </th>
<th class="thright">notes</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td><asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Id") %>' /></td>
<td><asp:CheckBox ID="chkIspresent1" runat="server" CssClass="checkbox" Checked="true" /></td>
<td><asp:CheckBox ID="chkIspresent2" runat="server" CssClass="checkbox" Checked="true" /></td>
<td><asp:CheckBox runat="server" CssClass="checkbox" ID="chkIsAbsent" /></td>
<td><asp:TextBox ID="txtNotes" runat="server"></asp:TextBox></td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Id") });
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
dt.Rows.Add(4);
rptStudents.DataSource = dt;
rptStudents.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptStudents.Items)
{
Label customerId = item.FindControl("lblCustomerId") as Label;
CheckBox isAbsent = item.FindControl("chkIsAbsent") as CheckBox;
TextBox notes= item.FindControl("txtNotes") as TextBox;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "INSERT INTO tblAttendance VALUES(@Id, @Present, @Note)";
cmd.Parameters.AddWithValue("@Id", customerId.Text.Trim());
cmd.Parameters.AddWithValue("@Present", isAbsent.Checked);
cmd.Parameters.AddWithValue("@Note", notes.Text.Trim());
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(0) {New DataColumn("Id")})
dt.Rows.Add(1)
dt.Rows.Add(2)
dt.Rows.Add(3)
dt.Rows.Add(4)
rptStudents.DataSource = dt
rptStudents.DataBind()
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each item As RepeaterItem In rptStudents.Items
Dim customerId As Label = TryCast(item.FindControl("lblCustomerId"), Label)
Dim isAbsent As CheckBox = TryCast(item.FindControl("chkIsAbsent"), CheckBox)
Dim notes As TextBox = TryCast(item.FindControl("txtNotes"), TextBox)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "INSERT INTO tblAttendance VALUES(@Id, @Present, @Note)"
cmd.Parameters.AddWithValue("@Id", customerId.Text.Trim())
cmd.Parameters.AddWithValue("@Present", isAbsent.Checked)
cmd.Parameters.AddWithValue("@Note", notes.Text.Trim())
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub