ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.[Solved] Records duplicating after adding in HTML Table using jQuery AJAX in ASP.Net<p>Hi <a class="username" rel="jovceka"> jovceka</a>,</p> <p>Make the table empty before adding record to it.</p> <p>$('#Tables-example').empty();</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(document).ready(function () { var currentPage = 1; Productbypage(currentPage); $(window).scroll(function () { if ($(window).scrollTop() == $(document).height() - $(window).height()) { currentPage += 1; Productbypage(currentPage); } }); $(&#34;#BtnaddProduct&#34;).click(function () { var ProdDetails = $(&#39;#TxtPdtdetail&#39;).val(); var OldPrice = $(&#39;#TxtoldOffprice&#39;).val(); var CurrentPrice = $(&#39;#TxtPdtCrrprice&#39;).val(); var OffDetails = $(&#39;#TxtPdtOffDetails&#39;).val(); var obj = {}; obj.OldPrice = OldPrice; obj.CurrentPrice = CurrentPrice; obj.OffDetails = OffDetails; obj.ProdDetails = ProdDetails; $.ajax({ type: &#34;POST&#34;, url: &#34;WebService.asmx/AddProduct&#34;, data: &#39;{data : &#39; + JSON.stringify(obj) + &#39; }&#39;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (data) { alert(&#34;success&#34;); Productbypage(currentPage); }, error: function (r) { alert(&#34;failure&#34;); } }); return false; }); }); function Productbypage(currentPageNumber) { var obj = {}; obj.pageNumber = currentPageNumber; obj.pageSize = 5; $.ajax({ type: &#34;POST&#34;, url: &#34;WebService.asmx/Productbypage&#34;, data: JSON.stringify(obj), contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (r) { var table = $(&#39;#Tables-example&#39;); $(&#39;#Tables-example&#39;).empty(); var rows = &#34;&#34;; $(r.d).each(function (index, emp) { var Proddetails = emp.Proddetails; var OldPrice = emp.OldPrice; var CurrentPrice = emp.CurrentPrice; var OffDetails = emp.OffDetails; rows += &#34;&lt;tr&gt;&lt;td&gt;&#34; + Proddetails + &#34;&lt;/td&gt;&lt;td&gt;&#34; + OldPrice + &#34;&lt;/td&gt;&lt;td&gt;&#34; + CurrentPrice + &#34;&lt;/td&gt;&lt;td&gt;&#34; + OffDetails + &#34;&lt;/td&gt;&lt;/tr&gt;&#34;; }); table.append(rows); }, error: function (r) { alert(&#34;Error while Showing update data&#34;); } }); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div class=&#34;row&#34;&gt; &lt;div class=&#34;col-lg-12&#34;&gt; &lt;div class=&#34;col-lg-12&#34;&gt; &lt;div class=&#34;panel panel-custom-horrible&#34;&gt; &lt;div class=&#34;panel panel-heading&#34;&gt; &lt;h3 class=&#34;panel-title&#34;&gt; &lt;i class=&#34;fa fa-bar-chart-o&#34;&gt;&lt;/i&gt;Add Products&lt;/h3&gt; &lt;/div&gt; &lt;div class=&#34;panel panel-body&#34;&gt; &lt;div class=&#34;form-group col-lg-4&#34;&gt; &lt;label&gt; Old price&lt;/label&gt; &lt;input type=&#34;text&#34; name=&#34;Old Off&#34; id=&#34;TxtoldOffprice&#34; class=&#34;form-control&#34; placeholder=&#34;Old price&#34; required=&#34;&#34; /&gt; &lt;/div&gt; &lt;div class=&#34;form-group col-lg-4&#34;&gt; &lt;label&gt; current price&lt;/label&gt; &lt;input type=&#34;text&#34; name=&#34;current OFF&#34; id=&#34;TxtPdtCrrprice&#34; class=&#34;form-control&#34; placeholder=&#34;current price&#34; required=&#34;&#34; /&gt; &lt;/div&gt; &lt;div class=&#34;form-group col-lg-4&#34;&gt; &lt;label&gt; Off Details&lt;/label&gt; &lt;input type=&#34;text&#34; name=&#34;off details&#34; id=&#34;TxtPdtOffDetails&#34; class=&#34;form-control&#34; placeholder=&#34;off details like percentage off&#34; required=&#34;&#34; /&gt; &lt;/div&gt; &lt;div class=&#34;form-group col-lg-8&#34;&gt; &lt;label&gt; product details&lt;/label&gt; &lt;textarea class=&#34;form-control&#34; rows=&#34;3&#34; id=&#34;TxtPdtdetail&#34;&gt;&lt;/textarea&gt; &lt;div class=&#34;form-group col-lg-8&#34;&gt; &lt;div style=&#34;float: right&#34;&gt; &lt;button class=&#34;btn btn-large btn-primary&#34; type=&#34;button&#34; id=&#34;BtnaddProduct&#34;&gt; Submit&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=&#34;col-lg-12&#34;&gt; &lt;div class=&#34;panel panel-custom-horrible custhead&#34;&gt; &lt;div class=&#34;panel panel-heading&#34;&gt; Product details &lt;/div&gt; &lt;div class=&#34;panel-body&#34;&gt; &lt;div class=&#34;dataTable_wrapper&#34;&gt; &lt;div id=&#34;Tables-example_wrapper&#34; class=&#34;dataTables_wrapper form-inline dt-bootstrap no-footer&#34;&gt; &lt;div class=&#34;row&#34;&gt; &lt;div class=&#34;table-responsive&#34; id=&#34;tableproduct&#34;&gt; &lt;table id=&#34;Tables-example&#34; class=&#34;table table-responsive table-striped table-bordered table-hover dataTable no-footer storetable&#34; role=&#34;grid&#34; aria-describedby=&#34;dataTables-example_info&#34;&gt; &lt;thead&gt; &lt;tr role=&#34;row&#34; class=&#34;srow&#34;&gt; &lt;th class=&#34;sorting&#34; tabindex=&#34;0&#34; aria-controls=&#34;dataTables-example&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; style=&#34;width: 110px;&#34; aria-label=&#34;CSS grade: activate to sort column ascending&#34;&gt; Product Details &lt;/th&gt; &lt;th class=&#34;sorting&#34; tabindex=&#34;0&#34; aria-controls=&#34;dataTables-example&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; style=&#34;width: 110px;&#34; aria-label=&#34;CSS grade: activate to sort column ascending&#34;&gt; Old price &lt;/th&gt; &lt;th class=&#34;sorting&#34; tabindex=&#34;0&#34; aria-controls=&#34;dataTables-example&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; style=&#34;width: 110px;&#34; aria-label=&#34;CSS grade: activate to sort column ascending&#34;&gt; Current price &lt;/th&gt; &lt;th class=&#34;sorting&#34; tabindex=&#34;0&#34; aria-controls=&#34;dataTables-example&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; style=&#34;width: 110px;&#34; aria-label=&#34;CSS grade: activate to sort column ascending&#34;&gt; Off Details &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;/table&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <p><strong>WebService.cs</strong></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Web.Services; using System.Configuration; using System.Data.SqlClient; using System.Data; [WebService(Namespace = &#34;http://tempuri.org/&#34;)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [System.Web.Services.WebMethod] public string AddProduct(Products data) { string message = string.Empty; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { SqlCommand cmd = new SqlCommand(&#34;sp_AddProduct&#34;); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(&#34;@Proddetails&#34;, data.Proddetails); cmd.Parameters.AddWithValue(&#34;@OldPrice&#34;, data.OldPrice); cmd.Parameters.AddWithValue(&#34;@CurrentPrice&#34;, data.CurrentPrice); cmd.Parameters.AddWithValue(&#34;@OffDetails&#34;, data.OffDetails); cmd.Connection = con; con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); if (i == 1) { message = &#34;true&#34;; } else { message = &#34;false&#34;; } con.Close(); return message; } } [System.Web.Services.WebMethod] public Array Productbypage(int pageNumber, int pageSize) { List&lt;Products&gt; details = new List&lt;Products&gt;(); string cs = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand(&#34;sp_pdt_page&#34;, con); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter() { ParameterName = &#34;@PageNumber&#34;, Value = pageNumber }); cmd.Parameters.Add(new SqlParameter() { ParameterName = &#34;@PageSize&#34;, Value = pageSize }); using (var sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; DataTable TableData = new DataTable(); TableData.Clear(); sda.Fill(TableData); foreach (DataRow dtrow in TableData.Rows) { Products user = new Products(); user.Productid = Convert.ToInt32(dtrow[&#34;Productid&#34;].ToString()); user.Proddetails = dtrow[&#34;Proddetails&#34;].ToString(); user.OldPrice = dtrow[&#34;OldPrice&#34;].ToString(); user.CurrentPrice = dtrow[&#34;CurrentPrice&#34;].ToString(); user.OffDetails = dtrow[&#34;OffDetails&#34;].ToString(); details.Add(user); } } } return details.ToArray(); } public class Products { public int Productid; public string Proddetails; public string OldPrice; public string CurrentPrice; public string OffDetails; } }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/522392/Solved-Records-duplicating-after-adding-in-HTML-Table-using-jQuery-AJAX-in-ASPNet/https://www.aspforums.net:443/Threads/522392/Solved-Records-duplicating-after-adding-in-HTML-Table-using-jQuery-AJAX-in-ASPNet/Fri, 14 Jun 2019 07:58:23 GMT