Hi,This is part of my initial post.
I am following up on that question because I am not receiving message that Data is saved successfully because the code is not inserting records to the database.
Can you please tell what I am missing or what I am doing wrong?
I have gotten this far because of the great help from this forum.
Thank you
<script type='text/javascript'> //<![CDATA[
window.onload=function(){
$(document).on('click', '.delete-row', function(){
$(this).closest('tr').remove();
});
$(document).on('click', '.add-row', function(){
var tr = $(this).closest('tr');
var field = [
'.employeename', '.employeetitle', '.employeeemail'
];
console.log(tr);
var flag = false;
for(var i = 0; i < field.length; i++) {
var ele = $.trim($(tr).find(field[i]).val());
console.log(ele);
console.log($(tr).find(field[i]));
if(!ele.length) {
flag = true;
$(tr).find(field[i]).addClass('error');
} else {
$(tr).find(field[i]).removeClass('error');
}
}
if (!flag) {
var html = "<tr><td><input name='employeename" + i + "' type='text' placeholder='Your name...' class='form-control employeename input-md'/> </td><td><input name='employeetitle" + i + "' type='text' placeholder='Your title...' class='employeetitle form-control input-md'></td><td><input name='employeeemail" + i + "' type='text' placeholder='Your email...' class='form-control input-md employeeemail'></td><td><a href='#' class='btn btn-warning add-row' style='background-color:#0093B2'><i class='glyphicon glyphicon-plus'></i></a><a href='#' class='btn btn-danger delete-row'><i class='glyphicon glyphicon-minus'></i></a></td></tr>";
$(html).insertAfter(tr);
}
});
$(document).on('click', '.add-row', function(){
var tr = $(this).closest('tr');
var field = [
'.sourcename', '.sourceaddress', '.sourceincome'
];
console.log(tr);
var flag = false;
for(var i = 0; i < field.length; i++) {
var ele = $.trim($(tr).find(field[i]).val());
console.log(ele);
console.log($(tr).find(field[i]));
if(!ele.length) {
flag = true;
$(tr).find(field[i]).addClass('error');
} else {
$(tr).find(field[i]).removeClass('error');
}
}
if (!flag) {
var html = "<tr><td><input name='sourcename" + i + "' type='text' placeholder='Source Name' class='form-control sourcename input-md'/> </td><td><input name='sourceaddress" + i + "' type='text' placeholder='Source address...' class='sourceaddress form-control input-md'></td><td><input name='sourceincome" + i + "' type='text' placeholder='Income...' class='form-control input-md sourceincome'></td><td><a href='#' class='btn btn-warning add-row' style='background-color:#0093B2'><i class='glyphicon glyphicon-plus'></i></a><a href='#' class='btn btn-danger delete-row'><i class='glyphicon glyphicon-minus'></i></a></td></tr>";
$(html).insertAfter(tr);
}
});
}//]]>
$(function () {
$('[id*=btnCreate]').click(function (event) {
event.preventDefault();
var details = [];
$('[id*=tab_logic1] tr:not(:has(th))').each(function () {
var obj = {};
obj.EName = $.trim($(this).find('input').eq(0).val());
obj.EAddress = $.trim($(this).find('input').eq(1).val());
obj.EIncome = $.trim($(this).find('input').eq(2).val());
details.push(obj);
});
$.ajax({
type: "POST",
url: "Default.aspx.aspx/SaveEmpDetails",
data: '{ Details :' + JSON.stringify(details) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
}
});
$('[id*=tab_logic2] tr:not(:has(th))').each(function () {
var obj = {};
obj.SOName = $.trim($(this).find('input').eq(0).val());
obj.SOAddress = $.trim($(this).find('input').eq(1).val());
obj.SOIncome = $.trim($(this).find('input').eq(2).val());
details.push(obj);
});
$.ajax({
type: "POST",
url: "Default.aspx.aspx/SaveSourceDetails",
data: '{ Details :' + JSON.stringify(details) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
}
});
return false;
});
});
</script>
<body>
<form id="form1" runat="server">
<div class="bs-example">
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic1" style="width:800px;">
<thead>
<tr >
<th class="text-center">
Employee Name
</th>
<th class="text-center">
Title
</th>
<th class="text-center">
Email
</th>
</tr>
</thead>
<tbody>
<tr id="addr1">
<td>
<asp:TextBox ID="employeename0" style="width:250px;" runat="server" placeholder="Your name..." CssClass="form-control employeename" required />
</td>
<td>
<asp:TextBox ID="employeetitle0" style="width:250px;" placeholder="Your title.." runat="server" CssClass="form-control employeetitle" required />
</td>
<td>
<asp:TextBox ID="employeeemail0" style="width:250px;" placeholder="Your email address..." runat="server" CssClass="form-control employeeemail" required />
</td>
</tr>
</tbody>
</table><br /><br />
<table class="table table-bordered table-hover" id="tab_logic2" style="width:800px;">
<thead>
<tr>
<th class="text-center">
Name
</th>
<th class="text-center">
Address
</th>
<th class="text-center">
Income
</th>
</tr>
</thead>
<tbody>
<tr id="addr2">
<td>
<asp:TextBox ID="sourcename0" style="width:250px;" placeholder="Source Name..." runat="server" CssClass="form-control sourcename" required />
</td>
<td>
<asp:TextBox ID="sourceaddress0" style="width:250px;" placeholder="Source Address..." runat="server" CssClass="form-control sourceaddress" required />
</td>
<td>
<asp:TextBox ID="sourceincome0" style="width:250px;" runat="server" placeholder='Income...' CssClass="form-control sourceincome" required />
</td>
<td>
<a href="#" class="btn btn-warning add-row" style="background-color:#0093B2">
<i class="glyphicon glyphicon-plus"></i>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
</body>
'/code behind
<System.Web.Services.WebMethod(EnableSession:=True)> _
Public Shared Function SaveEmpDetails(Details As List(Of EmpData)) As String
Using con = New SqlConnection(Constr)
If con.State = ConnectionState.Closed Then
con.Open()
End If
For Each data As EmpData In Details
Using cmd1 = New SqlCommand("INSERT INTO Employees(EmployeeName,empTitle,email) Values (@ename, @title,@email)")
cmd1.CommandType = CommandType.Text
cmd1.Parameters.AddWithValue("@ename", data.emplName)
cmd1.Parameters.AddWithValue("@title", data.emplTitle)
cmd1.Parameters.AddWithValue("@email", data.empMail)
cmd1.Connection = con
cmd1.ExecuteNonQuery()
Dim cmdGetKey As New SqlCommand("SELECT @@IDENTITY", con)
Dim ID As Integer = cmdGetKey.ExecuteScalar()
HttpContext.Current.Session("empID") = ID
End Using
Next
con.Close()
End Using
Return Nothing
End Function
<System.Web.Services.WebMethod(EnableSession:=True)> _
Public Shared Function SaveSourceDetails(Details As List(Of SODetails)) As String
Using con = New SqlConnection(Constr)
If con.State = ConnectionState.Closed Then
con.Open()
End If
For Each data As SODetails In Details
Using cmd = New SqlCommand("INSERT INTO SourceDetails(sourcename, sourceaddress, sourceincome,employeeID) VALUES(@sname, @saddress,@sincome, @ID)")
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@sname", data.mySource)
cmd.Parameters.AddWithValue("@saddress", data.mySAddress)
cmd.Parameters.AddWithValue("@sincome", data.mySIncome)
cmd.Parameters.AddWithValue("@ID", HttpContext.Current.Session("empID"))
' cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now)
cmd.Connection = con
cmd.ExecuteNonQuery()
End Using
Next
con.Close()
End Using
Return Nothing
End Function
Public Class EmpData
Public Property emplName() As String
Get
Return m_empName
End Get
Set(value As String)
m_empName = value
End Set
End Property
Private m_empName As String
Public Property emplTitle() As String
Get
Return m_empTitle
End Get
Set(value As String)
m_empTitle = value
End Set
End Property
Private m_empTitle As String
Public Property empMail() As String
Get
Return m_empMail
End Get
Set(value As String)
m_empMail = value
End Set
End Property
Private m_empMail As String
End Class
Public Class SODetails
Public Property mySource() As String
Get
Return m_mySource
End Get
Set(value As String)
m_mySource = value
End Set
End Property
Private m_mySource As String
Public Property mySAddress() As String
Get
Return m_mySAddress
End Get
Set(value As String)
m_mySAddress = value
End Set
End Property
Private m_mySAddress As String