Hi SajidHussa,
Check this example. Now please take its reference and correct your code.
For insert using jQuery AJAX refer below article.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add/Remove dynamic rows in HTML table</title>
<script language="javascript" 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[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
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].getElementsByTagName('INPUT')[0];
if (null != chkbox && true == chkbox.checked) {
if (rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch (e) {
alert(e);
}
}
function Save(tableID) {
var customers = new Array();
var table = document.getElementById(tableID);
for (var i = 0; i < table.rows.length; i++) {
var row = table.rows[i];
var customer = {};
customer.Name = row.cells[1].getElementsByTagName('INPUT')[0].value;
var ddl = row.cells[2].getElementsByTagName('OPTION');
for (var j = 0; j < ddl.length; j++) {
if (ddl[j].selected) {
customer.Country = ddl[j].text;
break;
}
}
customers.push(customer);
}
alert(JSON.stringify(customers));
// Make Ajax call to save in database.
}
</script>
</head>
<body>
<input type="button" value="Add Row" onclick="addRow('dataTable')" />
<input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<input type="button" value="Get Data" onclick="Save('dataTable')" />
<table id="dataTable" width="350px" border="1">
<tr>
<td><input type="checkbox" name="chk" /></td>
<td><input type="text" name="txt" /></td>
<td>
<select name="country" id="ddlCountries">
<option value="in">India</option>
<option value="de">Germany</option>
<option value="fr">France</option>
<option value="us">United States</option>
<option value="ch">Switzerland</option>
</select>
</td>
</tr>
</table>
</body>
</html>
Demo
For Ajax call refer below code.
Ajax function
$.ajax({
type: "POST",
url: "Default.aspx/InsertCustomers",
data: JSON.stringify({ customers: customers }),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
C#
[WebMethod]
public static void InsertCustomers(List<Customer> customers)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers VALUES(@Name,@Country)", con))
{
foreach (var cust in customers)
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", cust.Name);
cmd.Parameters.AddWithValue("@Country", cust.Country);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
public class Customer
{
public string Name { get; set; }
public string Country { get; set; }
}
VB.Net
<WebMethod()> _
Public Shared Sub InsertCustomers(ByVal customers As List(Of Customer))
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Customers VALUES(@Name,@Country)", con)
For Each cust In customers
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", cust.Name)
cmd.Parameters.AddWithValue("@Country", cust.Country)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
End Using
End Using
End Sub
Public Class Customer
Public Property Name As String
Public Property Country As String
End Class