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 file from server and record from database using WebService in AngularJS<p>Hi&nbsp;rani,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><span style="text-decoration: underline;"><strong>Database</strong></span></p> <p>For this example i have&nbsp;used table named tblFilesPath whose schema and data are defined as follows.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">CREATE TABLE [dbo].[tblFilesPath] ( Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL, Name NVARCHAR(50) NOT NULL, Path NVARCHAR(200) NOT NULL ) GO INSERT INTO tblFilesPath VALUES(&#39;Chrysanthemum.jpg&#39;,&#39;Images/Chrysanthemum.jpg&#39;) INSERT INTO tblFilesPath VALUES(&#39;Desert.jpg&#39;,&#39;Images/Desert.jpg&#39;)</pre> <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&gt; &lt;title&gt;Index&lt;/title&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/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) { GetAllFiles(); function GetAllFiles() { $http.post(&#34;WebService.asmx/BindFiles&#34;, { headers: { &#39;Content-Type&#39;: &#39;application/json&#39;} }) .then(function (response) { $scope.Files = eval(response.data.d); }); } // Delete File. $scope.DeleteData = function (Id, Name) { $http.post(&#34;WebServiceCS.asmx/DeleteFile&#34;, { id: Id }, { headers: { &#39;Content-Type&#39;: &#39;application/json&#39;} }) .then(function (response) { GetAllFiles(); }); } }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div ng-app=&#34;MyApp&#34; ng-controller=&#34;MyController&#34;&gt; &lt;table class=&#34;table table-striped table-bordered table-hover table-condensed&#34;&gt; &lt;tr&gt; &lt;th&gt;Id&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Photo&lt;/th&gt; &lt;th&gt;Action&lt;/th&gt; &lt;/tr&gt; &lt;tbody ng-repeat=&#34;m in Files&#34;&gt; &lt;tr&gt; &lt;td&gt;{{m.Id}}&lt;/td&gt; &lt;td&gt;{{m.Name}}&lt;/td&gt; &lt;td&gt;&lt;img alt=&#34;{{m.Name}}&#34; ng-src=&#34;{{m.Path}}&#34; style=&#34;height: 100px; width: 100px&#34; /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type=&#34;button&#34; value=&#34;Delete&#34; ng-click=&#34;DeleteData(m.Id)&#34; class=&#34;btn btn-danger&#34; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">WebService</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Web.Script.Serialization; using System.Web.Services; [WebService(Namespace = &#34;http://tempuri.org/&#34;)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [WebMethod] public string BindFiles() { return (new JavaScriptSerializer().Serialize(GetFiles())); } public List&lt;File&gt; GetFiles() { List&lt;File&gt; files = new List&lt;File&gt;(); using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM tblFilesPath&#34;)) { cmd.Connection = conn; conn.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { files.Add(new File { Id = Convert.ToInt32(sdr[&#34;Id&#34;]), Name = sdr[&#34;Name&#34;].ToString(), Path = sdr[&#34;Path&#34;].ToString() }); } } conn.Close(); } } return files; } [WebMethod] public bool DeleteFile(int id) { // Get details of File to be Deleted. File file = GetFiles().Where(x =&gt; x.Id == id).FirstOrDefault(); // Delete record from Database. string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(conString)) { using (SqlCommand cmd = new SqlCommand(&#34;DELETE FROM tblFilesPath WHERE Id = @Id&#34;)) { cmd.Connection = con; cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); con.Open(); int rowsAffected = cmd.ExecuteNonQuery(); con.Close(); if (rowsAffected &gt; 0) { // Delete file from Server Path. if (System.IO.File.Exists((Server.MapPath(&#34;~/&#34;) + file.Path).Replace(&#34;/&#34;, &#34;\\&#34;))) { System.IO.File.Delete(Server.MapPath(&#34;~/&#34;) + file.Path.Replace(&#34;/&#34;, &#34;\\&#34;)); } } return rowsAffected &gt; 0; } } } public class File { public int Id { get; set; } public string Name { get; set; } public string Path { get; set; } } } </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System Imports System.Collections.Generic Imports System.Configuration Imports System.Data.SqlClient Imports System.Linq Imports System.Web.Script.Serialization Imports System.Web.Services &#39; To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. &lt;System.Web.Script.Services.ScriptService()&gt; _ &lt;WebService(Namespace:=&#34;http://tempuri.org/&#34;)&gt; _ &lt;WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)&gt; _ &lt;Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()&gt; _ Public Class WebServiceVB Inherits System.Web.Services.WebService &lt;WebMethod()&gt; Public Function BindFiles() As String Return (New JavaScriptSerializer().Serialize(GetFiles())) End Function Public Function GetFiles() As List(Of File) Dim files As List(Of File) = New List(Of File)() Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM tblFilesPath&#34;) cmd.Connection = conn conn.Open() Using sdr As SqlDataReader = cmd.ExecuteReader() While sdr.Read() files.Add(New File With { .Id = Convert.ToInt32(sdr(&#34;Id&#34;)), .Name = sdr(&#34;Name&#34;).ToString(), .Path = sdr(&#34;Path&#34;).ToString() }) End While End Using conn.Close() End Using End Using Return files End Function &lt;WebMethod()&gt; Public Function DeleteFile(ByVal id As Integer) As Boolean &#39; Get details of File to be Deleted. Dim file As File = GetFiles().Where(Function(x) x.Id = id).FirstOrDefault() &#39; Delete record from Database. Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(conString) Using cmd As SqlCommand = New SqlCommand(&#34;DELETE FROM tblFilesPath WHERE Id = @Id&#34;) cmd.Connection = con cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) con.Open() Dim rowsAffected As Integer = cmd.ExecuteNonQuery() con.Close() If rowsAffected &gt; 0 Then &#39; Delete file from Server Path. If System.IO.File.Exists((Server.MapPath(&#34;~/&#34;) &amp; file.Path).Replace(&#34;/&#34;, &#34;\&#34;)) Then System.IO.File.Delete(Server.MapPath(&#34;~/&#34;) &amp; file.Path.Replace(&#34;/&#34;, &#34;\&#34;)) End If End If Return rowsAffected &gt; 0 End Using End Using End Function Public Class File Public Property Id As Integer Public Property Name As String Public Property Path As String End Class End Class</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/Z3yI2fC.gif" alt="" width="472" height="288" /></span></strong></p>https://www.aspforums.net:443/Threads/288094/Delete-file-from-server-and-record-from-database-using-WebService-in-AngularJS/https://www.aspforums.net:443/Threads/288094/Delete-file-from-server-and-record-from-database-using-WebService-in-AngularJS/Thu, 18 Jul 2019 04:02:08 GMT