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 data from Database and Image from Folder (Directory) using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="itsme"> itsme</a>,</p> <p>Refer below it's working.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView runat=&#34;server&#34; ID=&#34;gvImage&#34; AutoGenerateColumns=&#34;false&#34; OnRowCancelingEdit=&#34;gvImage_RowCancelingEdit&#34; DataKeyNames=&#34;Id&#34; OnRowEditing=&#34;gvImage_RowEditing&#34; OnRowUpdating=&#34;gvImage_RowUpdating&#34; OnRowDeleting=&#34;gvImage_RowDeleting&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;Business Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblId&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&#34;txt_Id&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblname&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&#34;txt_name&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39;&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Image&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Image ID=&#34;Image1&#34; runat=&#34;server&#34; ImageUrl=&#39;&lt;%# Eval(&#34;Path&#34;) %&gt;&#39; eight=&#34;80px&#34; Width=&#34;80px&#34; /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:Image ID=&#34;img_user&#34; runat=&#34;server&#34; ImageUrl=&#39;&lt;%# Eval(&#34;Path&#34;) %&gt;&#39; Height=&#34;80px&#34; Width=&#34;80px&#34; /&gt;&lt;br /&gt; &lt;asp:FileUpload ID=&#34;FileUpload1&#34; runat=&#34;server&#34; /&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;lbl_edit&#34; runat=&#34;server&#34; CommandName=&#34;Edit&#34;&gt;Edit&lt;/asp:LinkButton&gt; &lt;asp:LinkButton ID=&#34;lbl_delete&#34; runat=&#34;server&#34; CommandName=&#34;Delete&#34;&gt;Delete&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:LinkButton ID=&#34;lbl_update&#34; runat=&#34;server&#34; CommandName=&#34;Update&#34;&gt;Update&lt;/asp:LinkButton&gt; &lt;asp:LinkButton ID=&#34;lbl_cancle&#34; runat=&#34;server&#34; CommandName=&#34;Cancel&#34;&gt;Cancel&lt;/asp:LinkButton&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Label ID=&#34;lblResult&#34; runat=&#34;server&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Data; using System.Configuration; using System.IO;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.IO Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">SqlConnection con; SqlDataAdapter da; DataSet ds; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { ImageData(); } } protected void ImageData() { con = new SqlConnection(constr); con.Open(); da = new SqlDataAdapter(&#34;SELECT * FROM tblfilespath&#34;, con); ds = new DataSet(); da.Fill(ds); gvImage.DataSource = ds; gvImage.DataBind(); } protected void gvImage_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvImage.EditIndex = -1; ImageData(); } protected void gvImage_RowEditing(object sender, GridViewEditEventArgs e) { gvImage.EditIndex = e.NewEditIndex; ImageData(); } protected void gvImage_RowUpdating(object sender, GridViewUpdateEventArgs e) { string Id = gvImage.DataKeys[e.RowIndex].Value.ToString(); TextBox txt_BName = (TextBox)gvImage.Rows[e.RowIndex].FindControl(&#34;txt_BName&#34;); TextBox txt_city = (TextBox)gvImage.Rows[e.RowIndex].FindControl(&#34;txt_city&#34;); TextBox txt_name = (TextBox)gvImage.Rows[e.RowIndex].FindControl(&#34;txt_name&#34;); TextBox txt_toll = (TextBox)gvImage.Rows[e.RowIndex].FindControl(&#34;txt_toll&#34;); TextBox txt_link = (TextBox)gvImage.Rows[e.RowIndex].FindControl(&#34;txt_link&#34;); TextBox txt_pay = (TextBox)gvImage.Rows[e.RowIndex].FindControl(&#34;txt_pay&#34;); TextBox txt_email = (TextBox)gvImage.Rows[e.RowIndex].FindControl(&#34;txt_email&#34;); FileUpload FileUpload1 = (FileUpload)gvImage.Rows[e.RowIndex].FindControl(&#34;FileUpload1&#34;); con = new SqlConnection(constr); string path = &#34;~/uploads/&#34;; if (FileUpload1.HasFile) { path += FileUpload1.FileName; FileUpload1.SaveAs(MapPath(path)); } else { Image img = (Image)gvImage.Rows[e.RowIndex].FindControl(&#34;img_user&#34;); path = img.ImageUrl; } SqlCommand cmd = new SqlCommand(&#34;update Q_SP_Afterlogin set Bussiness_name=&#39;&#34; + txt_BName.Text + &#34;&#39;,city=&#39;&#34; + txt_city.Text + &#34;&#39;,name=&#39;&#34; + txt_name.Text + &#34;&#39;,toll_free=&#39;&#34; + txt_toll.Text + &#34;&#39;,website_link=&#39;&#34; + txt_link.Text + &#34;&#39;,payment_mode=&#39;&#34; + txt_pay.Text + &#34;&#39;,email=&#39;&#34; + txt_email.Text + &#34;&#39;,Images=&#39;&#34; + path + &#34;&#39; where Id=&#34; + Id + &#34;&#34;, con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); gvImage.EditIndex = -1; ImageData(); } protected void gvImage_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridViewRow row = (GridViewRow)gvImage.Rows[e.RowIndex]; Label Id = (Label)row.FindControl(&#34;lblId&#34;); Label name = (Label)row.FindControl(&#34;lblname&#34;); con = new SqlConnection(constr); con.Open(); SqlCommand cmd = new SqlCommand(&#34;delete FROM tblfilespath where Id=&#39;&#34; + Convert.ToInt32(gvImage.DataKeys[e.RowIndex].Value.ToString()) + &#34;&#39;&#34;, con); cmd.ExecuteNonQuery(); con.Close(); ImageDeleteFromFolder(name.Text); ImageData(); } protected void ImageDeleteFromFolder(string Images) { string file_name = Images; string path = Server.MapPath(&#34;~/uploads/&#34;); FileInfo file = new FileInfo(path + file_name); if (file.Exists) { file.Delete(); lblResult.Text = &#34; file deleted successfully&#34;; lblResult.ForeColor = System.Drawing.Color.Green; } else { lblResult.Text = &#34; This file does not exists &#34;; lblResult.ForeColor = System.Drawing.Color.Red; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private con As SqlConnection Private da As SqlDataAdapter Private ds As DataSet Private constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then ImageData() End If End Sub Protected Sub ImageData() con = New SqlConnection(constr) con.Open() da = New SqlDataAdapter(&#34;SELECT * FROM tblfilespath&#34;, con) ds = New DataSet() da.Fill(ds) gvImage.DataSource = ds gvImage.DataBind() End Sub Protected Sub gvImage_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) gvImage.EditIndex = -1 ImageData() End Sub Protected Sub gvImage_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) gvImage.EditIndex = e.NewEditIndex ImageData() End Sub Protected Sub gvImage_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Dim Id As String = gvImage.DataKeys(e.RowIndex).Value.ToString() Dim txt_BName As TextBox = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;txt_BName&#34;), TextBox) Dim txt_city As TextBox = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;txt_city&#34;), TextBox) Dim txt_name As TextBox = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;txt_name&#34;), TextBox) Dim txt_toll As TextBox = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;txt_toll&#34;), TextBox) Dim txt_link As TextBox = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;txt_link&#34;), TextBox) Dim txt_pay As TextBox = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;txt_pay&#34;), TextBox) Dim txt_email As TextBox = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;txt_email&#34;), TextBox) Dim FileUpload1 As FileUpload = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;FileUpload1&#34;), FileUpload) con = New SqlConnection(constr) Dim path As String = &#34;~/uploads/&#34; If FileUpload1.HasFile Then path += FileUpload1.FileName FileUpload1.SaveAs(MapPath(path)) Else Dim img As Image = CType(gvImage.Rows(e.RowIndex).FindControl(&#34;img_user&#34;), Image) path = img.ImageUrl End If Dim cmd As SqlCommand = New SqlCommand(&#34;update Q_SP_Afterlogin set Bussiness_name=&#39;&#34; &amp; txt_BName.Text &amp; &#34;&#39;,city=&#39;&#34; + txt_city.Text &amp; &#34;&#39;,name=&#39;&#34; + txt_name.Text &amp; &#34;&#39;,toll_free=&#39;&#34; + txt_toll.Text &amp; &#34;&#39;,website_link=&#39;&#34; + txt_link.Text &amp; &#34;&#39;,payment_mode=&#39;&#34; + txt_pay.Text &amp; &#34;&#39;,email=&#39;&#34; + txt_email.Text &amp; &#34;&#39;,Images=&#39;&#34; &amp; path &amp; &#34;&#39; where Id=&#34; &amp; Id &amp; &#34;&#34;, con) con.Open() cmd.ExecuteNonQuery() con.Close() gvImage.EditIndex = -1 ImageData() End Sub Protected Sub gvImage_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Dim row As GridViewRow = CType(gvImage.Rows(e.RowIndex), GridViewRow) Dim Id As Label = CType(row.FindControl(&#34;lblId&#34;), Label) Dim name As Label = CType(row.FindControl(&#34;lblname&#34;), Label) con = New SqlConnection(constr) con.Open() Dim cmd As SqlCommand = New SqlCommand(&#34;delete FROM tblfilespath where Id=&#39;&#34; &amp; Convert.ToInt32(gvImage.DataKeys(e.RowIndex).Value.ToString()) &amp; &#34;&#39;&#34;, con) cmd.ExecuteNonQuery() con.Close() ImageDeleteFromFolder(name.Text) ImageData() End Sub Protected Sub ImageDeleteFromFolder(ByVal Images As String) Dim file_name As String = Images Dim path As String = Server.MapPath(&#34;~/uploads/&#34;) Dim file As FileInfo = New FileInfo(path &amp; file_name) If file.Exists Then file.Delete() lblResult.Text = &#34; file deleted successfully&#34; lblResult.ForeColor = System.Drawing.Color.Green Else lblResult.Text = &#34; This file does not exists &#34; lblResult.ForeColor = System.Drawing.Color.Red End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/hXBRLsR.gif" alt="" width="330" height="458" /></p>https://www.aspforums.net:443/Threads/121149/Delete-data-from-Database-and-Image-from-Folder-Directory-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/121149/Delete-data-from-Database-and-Image-from-Folder-Directory-using-C-and-VBNet-in-ASPNet/Wed, 24 Jul 2019 04:47:37 GMT