Hi saranjoe,
Please refer below sample.
HTML
<asp:TextBox ID="txtId" runat="server" />
<br />
<table>
<tr>
<td>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
</td>
<td>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:TextBox ID="txtTextBox1" runat="server" Text='<%# Eval("Value") %>' />
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
</td>
</tr>
</table>
<asp:Button Text="Save" runat="server" OnClick="Save" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
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("Name") });
dt.Rows.Add("X");
dt.Rows.Add("Y");
Repeater2.DataSource = dt;
Repeater2.DataBind();
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[1] { new DataColumn("Value") });
for (int i = 0; i < 2; i++)
{
dt1.Rows.Add("");
}
Repeater1.DataSource = dt1;
Repeater1.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
int i = 0;
foreach (RepeaterItem item in Repeater2.Items)
{
string id = txtId.Text.Trim();
string factor = (item.FindControl("lblName") as Label).Text.Trim();
TextBox txtValue = (TextBox)Repeater1.Items[i].FindControl("txtTextBox1");
string conString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
string query = "INSERT INTO Disease_etiology VALUES (@Disease_id, @Factor, @Description)";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Disease_id", id);
cmd.Parameters.AddWithValue("@Factor", factor);
cmd.Parameters.AddWithValue("@Description", txtValue.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
i++;
}
}
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("Name")})
dt.Rows.Add("X")
dt.Rows.Add("Y")
Repeater2.DataSource = dt
Repeater2.DataBind()
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn(0) {New DataColumn("Value")})
For i As Integer = 0 To 2 - 1
dt1.Rows.Add("")
Next
Repeater1.DataSource = dt1
Repeater1.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = 0
For Each item As RepeaterItem In Repeater2.Items
Dim id As String = txtId.Text.Trim()
Dim factor As String = (TryCast(item.FindControl("lblName"), Label)).Text.Trim()
Dim txtValue As TextBox = CType(Repeater1.Items(i).FindControl("txtTextBox1"), TextBox)
Dim conString As String = ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString
Dim query As String = "INSERT INTO Disease_etiology VALUES (@Disease_id, @Factor, @Description)"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
cmd.Parameters.AddWithValue("@Disease_id", id)
cmd.Parameters.AddWithValue("@Factor", factor)
cmd.Parameters.AddWithValue("@Description", txtValue.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
i += 1
Next
End Sub