Hi Ghanshyam,
Refer below sample code.
HTML
MasterPage.master
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[1].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
switch (newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[1];
if (chkbox != null && chkbox.checked == true) {
if (rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch (e) {
alert(e);
}
}
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
CS.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="CS.aspx.cs" Inherits="CS" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div class="container-fluid" style="padding-top: 5em;">
<div class="row">
<div class="col-sm-6">
<table class="table bg-light">
<tr>
<th>
<label class="col-form-label-sm">
Case Name</label>
</th>
<th>
<input type="text" class="form-control-plaintext" placeholder="Enter case name" />
</th>
</tr>
<tr>
<th>
<label class="col-form-label-sm">
Container ID</label>
</th>
<th>
<input type="text" class="form-control-plaintext" placeholder="Enter container ID" />
</th>
</tr>
</table>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-12">
<table id="dataTable1" class="table-borderless table-hover table" style="border-bottom: 1px solid lightgrey;">
<tr style="border-bottom: 1px solid lightgrey; border-top: 1px solid lightgrey; text-align: center;"
class="bg-light">
<th style="font-size: 12px; line-height: 1px;">
#
</th>
<th style="font-size: 12px; line-height: 1px;">
Process
</th>
<th style="font-size: 12px; line-height: 1px;">
User
</th>
<th style="font-size: 12px; line-height: 1px;">
Person
</th>
<th style="font-size: 12px; line-height: 1px;">
Machine
</th>
<th style="font-size: 12px; line-height: 1px;">
Start Time
</th>
<th style="font-size: 12px; line-height: 1px;">
End Time
</th>
</tr>
<tr>
<td>
<input type="checkbox" name="chk" />
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="form-control">
<asp:ListItem></asp:ListItem>
<asp:ListItem Text="Tiffing"></asp:ListItem>
<asp:ListItem Text="Loading"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="form-control">
<asp:ListItem></asp:ListItem>
<asp:ListItem Text="Ashish Parekh"></asp:ListItem>
<asp:ListItem Text="Chirag Sharma"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" CssClass="form-control">
<asp:ListItem></asp:ListItem>
<asp:ListItem Text="Sudhir Barot"></asp:ListItem>
<asp:ListItem Text="Rakshit Panchal"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="DropDownList4" runat="server" CssClass="form-control">
<asp:ListItem></asp:ListItem>
<asp:ListItem Text="172.30.0.82"></asp:ListItem>
<asp:ListItem Text="172.30.0.83"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txt1" runat="server" CssClass="form-control"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txt2" runat="server" CssClass="form-control"></asp:TextBox>
</td>
</tr>
</table>
</div>
<div class="row">
<div class="col-12" style="margin-left: 15px;">
<input type="button" class="btn btn-success active" value="Add Activity" onclick="addRow('dataTable1')" />
<input type="button" class="btn btn-danger active" value="Delete Activity" onclick="deleteRow('dataTable1')" />
<asp:Button runat="server" ID="Save" CssClass="btn btn-info active" Text="Save" OnClick="Save_Click" />
</div>
</div>
</div>
</div>
<asp:GridView runat="server" ID="GridView1" />
</asp:Content>
Namespace
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 Save_Click(object sender, EventArgs e)
{
string[] ddl1 = Request.Form.GetValues(DropDownList1.UniqueID);
string[] ddl2 = Request.Form.GetValues(DropDownList2.UniqueID);
string[] ddl3 = Request.Form.GetValues(DropDownList3.UniqueID);
string[] ddl4 = Request.Form.GetValues(DropDownList4.UniqueID);
string[] textbox1 = Request.Form.GetValues(txt1.UniqueID);
string[] textbox2 = Request.Form.GetValues(txt2.UniqueID);
int length = 0;
if (ddl1.Length > length)
{
length = ddl1.Length;
}
if (ddl2.Length > length)
{
length = ddl2.Length;
}
if (ddl3.Length > length)
{
length = ddl3.Length;
}
if (ddl4.Length > length)
{
length = ddl4.Length;
}
if (textbox1.Length > length)
{
length = textbox1.Length;
}
if (textbox2.Length > length)
{
length = textbox2.Length;
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
for (int i = 0; i < length - 1; i++)
{
cmd = new SqlCommand("INSERT INTO tbltest1(process,users,person,machine,strTime,endTime) VALUES(@process,@users,@person,@machine,@strTime,@endTime)", con);
cmd.Parameters.AddWithValue("@process", ddl1.Length > i ? ddl1[i] : "");
cmd.Parameters.AddWithValue("@users", ddl2.Length > i ? ddl2[i] : "");
cmd.Parameters.AddWithValue("@person", ddl3.Length > i ? ddl3[i] : "");
cmd.Parameters.AddWithValue("@machine", ddl4.Length > i ? ddl4[i] : "");
cmd.Parameters.AddWithValue("@strTime", textbox1.Length > i ? textbox1[i] : "");
cmd.Parameters.AddWithValue("@endTime", textbox2.Length > i ? textbox2[i] : "");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
cmd = new SqlCommand("SELECT * FROM tbltest1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub Save_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl1 As String() = Request.Form.GetValues(DropDownList1.UniqueID)
Dim ddl2 As String() = Request.Form.GetValues(DropDownList2.UniqueID)
Dim ddl3 As String() = Request.Form.GetValues(DropDownList3.UniqueID)
Dim ddl4 As String() = Request.Form.GetValues(DropDownList4.UniqueID)
Dim textbox1 As String() = Request.Form.GetValues(txt1.UniqueID)
Dim textbox2 As String() = Request.Form.GetValues(txt2.UniqueID)
Dim length As Integer = 0
If ddl1.Length > length Then
length = ddl1.Length
End If
If ddl2.Length > length Then
length = ddl2.Length
End If
If ddl3.Length > length Then
length = ddl3.Length
End If
If ddl4.Length > length Then
length = ddl4.Length
End If
If textbox1.Length > length Then
length = textbox1.Length
End If
If textbox2.Length > length Then
length = textbox2.Length
End If
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand()
For i As Integer = 0 To length - 1 - 1
cmd = New SqlCommand("INSERT INTO tbltest1(process,users,person,machine,strTime,endTime) VALUES(@process,@users,@person,@machine,@strTime,@endTime)", con)
cmd.Parameters.AddWithValue("@process", If(ddl1.Length > i, ddl1(i), ""))
cmd.Parameters.AddWithValue("@users", If(ddl2.Length > i, ddl2(i), ""))
cmd.Parameters.AddWithValue("@person", If(ddl3.Length > i, ddl3(i), ""))
cmd.Parameters.AddWithValue("@machine", If(ddl4.Length > i, ddl4(i), ""))
cmd.Parameters.AddWithValue("@strTime", If(textbox1.Length > i, textbox1(i), ""))
cmd.Parameters.AddWithValue("@endTime", If(textbox2.Length > i, textbox2(i), ""))
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
cmd = New SqlCommand("SELECT * FROM tbltest1", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot
