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.Retain (Keep) previous uploaded Image if no file selected in ASP.Net FileUpload control using C# and VB.Net<p>Hi <a class="username" rel="itsme"> itsme</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div style=&#34;scrollbar-highlight-color: aqua&#34; onscroll=&#34;true&#34;&gt; &lt;table align=&#34;center&#34; style=&#34;position: relative; top: 20px;&#34;&gt; &lt;tr&gt; &lt;td&gt; &lt;table align=&#34;center&#34;&gt; &lt;tr&gt; &lt;td&gt; Image Name : &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txt_img&#34; runat=&#34;server&#34; MaxLength=&#34;200&#34; Width=&#34;250px&#34;&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Image : &lt;/td&gt; &lt;td&gt; &lt;asp:Image ID=&#34;Image1&#34; runat=&#34;server&#34; Height=&#34;80px&#34; Width=&#34;100px&#34; /&gt; &lt;asp:FileUpload ID=&#34;FileUpload1&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan=&#34;2&#34; align=&#34;center&#34;&gt; &lt;asp:Button ID=&#34;btnUpdate&#34; runat=&#34;server&#34; Text=&#34;Update&#34; OnClick=&#34;btnUpdate_Click&#34; /&gt; &lt;asp:Button ID=&#34;btnClear&#34; runat=&#34;server&#34; Text=&#34;Clear&#34; OnClick=&#34;btnClear_Click&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align=&#34;center&#34;&gt; &lt;br /&gt; &lt;asp:Label ID=&#34;lblMessage&#34; runat=&#34;server&#34; EnableViewState=&#34;false&#34; ForeColor=&#34;Blue&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:GridView ID=&#34;gvDepartments&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; ShowHeaderWhenEmpty=&#34;True&#34; EmptyDataText=&#34;No Records Found&#34; GridLines=&#34;both&#34; CssClass=&#34;gv&#34; EmptyDataRowStyle-ForeColor=&#34;Red&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;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;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34; Hours of Open&#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;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Image&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Image ID=&#34;img&#34; runat=&#34;server&#34; ImageUrl=&#39;&lt;%# Eval(&#34;Path&#34;) %&gt;&#39; Height=&#34;80px&#34; Width=&#34;100px&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Action&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Button ID=&#34;btnEdit&#34; runat=&#34;server&#34; Text=&#34;Edit&#34; OnClick=&#34;btnEdit_Click&#34; /&gt; &lt;asp:Button ID=&#34;btnDelete&#34; runat=&#34;server&#34; Text=&#34;Delete&#34; OnClientClick=&#34;return confirm(&#39;Are you sure? want to delete the department.&#39;);&#34; /&gt; &lt;asp:Label ID=&#34;Id&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Id&#34;) %&gt;&#39; Visible=&#34;false&#34;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type=&#34;hidden&#34; runat=&#34;server&#34; id=&#34;Id&#34; /&gt; &lt;/div&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; using System.Data.SqlClient; using System.Configuration; using System.IO;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; if (!this.IsPostBack) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BindGrid(); &nbsp;&nbsp;&nbsp; } } private void BindGrid() { &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; &nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand(&#34;SELECT Id, Name, Path FROM tblFilesPath&#34;, con); &nbsp;&nbsp;&nbsp; SqlDataAdapter da = new SqlDataAdapter(cmd); &nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp; da.Fill(dt); &nbsp;&nbsp;&nbsp; gvDepartments.DataSource = dt; &nbsp;&nbsp;&nbsp; gvDepartments.DataBind(); } protected void btnUpdate_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; string path = &#34;~/uploads/&#34;; &nbsp;&nbsp;&nbsp; string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); &nbsp;&nbsp;&nbsp; if (FileUpload1.HasFile) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; path += FileUpload1.FileName; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileUpload1.SaveAs(MapPath(path));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; path = Image1.ImageUrl; &nbsp;&nbsp;&nbsp; } UpdateFile(path); &nbsp;&nbsp;&nbsp; BindGrid(); &nbsp;&nbsp;&nbsp;&nbsp;ClearControls(); &nbsp;&nbsp;&nbsp; lblMessage.Text = &#34;Updated Successfully.&#34;; } private void UpdateFile(string path) { &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; &nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand(&#34;UPDATE tblFilesPath SET Path =@Path WHERE Id =@Id&#34;, con); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Id&#34;, Id.Value); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Path&#34;, path); &nbsp;&nbsp;&nbsp; con.Open(); &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp; con.Close(); } private void ClearControls() { &nbsp;&nbsp;&nbsp; txt_img.Text = &#34;&#34;; &nbsp;&nbsp;&nbsp; Image1.ImageUrl = &#34;&#34;; } protected void btnClear_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; ClearControls(); } protected void btnEdit_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; try &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Button btn = sender as Button; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridViewRow grow = btn.NamingContainer as GridViewRow; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Id.Value = (grow.FindControl(&#34;Id&#34;) as Label).Text; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt_img.Text = (grow.FindControl(&#34;lblName&#34;) as Label).Text; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Image img = (grow.FindControl(&#34;img&#34;) as Image); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Image1.ImageUrl = img.ImageUrl; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnUpdate.Visible = true; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; catch &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Not Me.IsPostBack Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BindGrid() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Private Sub BindGrid() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT Id, Name, Path FROM tblFilesPath&#34;, con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(dt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvDepartments.DataSource = dt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvDepartments.DataBind() &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim path As String = &#34;~/uploads/&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If FileUpload1.HasFile Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; path += FileUpload1.FileName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileUpload1.SaveAs(MapPath(path)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; path = Image1.ImageUrl &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If UpdateFile(path) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BindGrid() ClearControls() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lblMessage.Text = &#34;Updated Successfully.&#34; &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Private Sub UpdateFile(ByVal path As String) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim cmd As SqlCommand = New SqlCommand(&#34;UPDATE tblFilesPath SET Path =@Path WHERE Id =@Id&#34;, con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Id&#34;, Id.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Path&#34;, path) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close() &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Private Sub ClearControls() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt_img.Text = &#34;&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Image1.ImageUrl = &#34;&#34; &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ClearControls() &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Try &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim btn As Button = TryCast(sender, Button) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim grow As GridViewRow = TryCast(btn.NamingContainer, GridViewRow) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Id.Value = (TryCast(grow.FindControl(&#34;Id&#34;), Label)).Text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt_img.Text = (TryCast(grow.FindControl(&#34;lblName&#34;), Label)).Text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim img As Image = (TryCast(grow.FindControl(&#34;img&#34;), Image)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Image1.ImageUrl = img.ImageUrl &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnUpdate.Visible = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Catch &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Try &nbsp;&nbsp;&nbsp; End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/hlrPs8t.gif" alt="" width="315" height="546" /></p>https://www.aspforums.net:443/Threads/121019/Retain-Keep-previous-uploaded-Image-if-no-file-selected-in-ASPNet-FileUpload-control-using-C-and-VBNet/https://www.aspforums.net:443/Threads/121019/Retain-Keep-previous-uploaded-Image-if-no-file-selected-in-ASPNet-FileUpload-control-using-C-and-VBNet/Mon, 29 Jul 2019 07:37:39 GMT