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.Delete multiple Rows using CheckBox in AngularJS in ASP.Net<p>Hi&nbsp;rani,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>I have made use of the following table Customers with the schema as follows.</p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=f18ac914-bc9b-437a-88e2-bd640ce05282.png" alt="" width="384" height="137" /></p> <p>I have already inserted few records in the table.</p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=b736972b-595c-4656-ab75-976e054877c7.png" alt="" width="340" height="156" /></p> <p>You can download the database table SQL by clicking the download link below.</p> <p><a href="https://www.aspsnippets.com/DownloadFile.aspx?File=Customers_Table_AutoIncrement.sql">Download SQL file</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></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;https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; var app = angular.module(&#39;MyApp&#39;, []); app.controller(&#39;MyController&#39;, function ($scope, $http, $window) { PopulateCustomers(); function PopulateCustomers() { $http.post(&#34;Default.aspx/BindCustomers&#34;, { headers: { &#39;Content-Type&#39;: &#39;application/json&#39;} }) .then(function (response) { $scope.Customers = eval(response.data.d); }); } $scope.Delete = function () { var selected = &#34;&#34;; for (var i = 0; i &lt; $scope.Customers.length; i++) { if ($scope.Customers[i].Selected != undefined &amp;&amp; $scope.Customers[i].Selected) { selected += $scope.Customers[i].Id + &#34;,&#34;; } } $http.post(&#34;Default.aspx/Delete&#34;, { ids: selected }, { headers: { &#39;Content-Type&#39;: &#39;application/json&#39;} }) .then(function (response) { PopulateCustomers(); }); } }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div ng-app=&#34;MyApp&#34; ng-controller=&#34;MyController&#34;&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Action&lt;/th&gt; &lt;th&gt;Id&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Country&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr ng-repeat=&#34;customer in Customers&#34;&gt; &lt;td&gt;&lt;input type=&#34;checkbox&#34; name=&#34;Selected&#34; ng-model=&#34;customer.Selected&#34; /&gt;&lt;/td&gt; &lt;td&gt;{{ customer.Id }}&lt;/td&gt; &lt;td&gt;{{ customer.Name }}&lt;/td&gt; &lt;td&gt;{{ customer.Country }}&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;br /&gt; &lt;input type=&#34;button&#34; value=&#34;Delete Selected&#34; ng-click=&#34;Delete()&#34; /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces </span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Web.Script.Serialization; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Collections.Generic Imports System.Configuration Imports System.Data.SqlClient Imports System.Web.Script.Serialization Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">[WebMethod] public static string BindCustomers() { List&lt;object&gt; customers = new List&lt;object&gt;(); string sql = &#34;SELECT * FROM Customers&#34;; using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand(sql)) { cmd.Connection = conn; conn.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { customers.Add(new { Id = sdr[&#34;CustomerId&#34;], Name = sdr[&#34;Name&#34;], Country = sdr[&#34;Country&#34;] }); } } conn.Close(); } return (new JavaScriptSerializer().Serialize(customers)); } } [WebMethod] public static void Delete(string ids) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand(&#34;DELETE FROM Customers WHERE CustomerID IN (&#34; + ids.TrimEnd(&#39;,&#39;) + &#34;)&#34;)) { cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">&lt;WebMethod()&gt; Public Shared Function BindCustomers() As String Dim customers As List(Of Object) = New List(Of Object)() Dim sql As String = &#34;SELECT * FROM Customers&#34; Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand(sql) cmd.Connection = conn conn.Open() Using sdr As SqlDataReader = cmd.ExecuteReader() While sdr.Read() customers.Add(New With { .Id = sdr(&#34;CustomerId&#34;), .Name = sdr(&#34;Name&#34;), .Country = sdr(&#34;Country&#34;) }) End While End Using conn.Close() End Using Return (New JavaScriptSerializer().Serialize(customers)) End Using End Function &lt;WebMethod()&gt; Public Shared Sub Delete(ByVal ids As String) Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand(&#34;DELETE FROM Customers WHERE CustomerID IN (&#34; &amp; ids.TrimEnd(&#34;,&#34;c) &amp; &#34;)&#34;) cmd.Connection = conn conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/XYgzG6c.gif" alt="" width="335" height="265" /></span></strong></p>https://www.aspforums.net:443/Threads/824183/Delete-multiple-Rows-using-CheckBox-in-AngularJS-in-ASPNet/https://www.aspforums.net:443/Threads/824183/Delete-multiple-Rows-using-CheckBox-in-AngularJS-in-ASPNet/Thu, 11 Jul 2019 04:52:49 GMT