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.Insert (Save) unique file in Folder (Directory) and Path in Database using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="chetan"> chetan</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:FileUpload runat=&#34;server&#34; ID=&#34;filemailerupl&#34; /&gt; &lt;asp:Button Text=&#34;Upload&#34; runat=&#34;server&#34; OnClick=&#34;Upload&#34; /&gt; &lt;hr /&gt; &lt;asp:GridView ID=&#34;gvImages&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Image Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:ImageField DataImageUrlField=&#34;Path&#34; HeaderText=&#34;Image&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Configuration; using System.Data.SqlClient; using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Data.SqlClient Imports System.Data</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) { if (!this.IsPostBack) { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { using (SqlDataAdapter sda = new SqlDataAdapter(&#34;SELECT * FROM tblFilespath&#34;, conn)) { DataTable dt = new DataTable(); sda.Fill(dt); gvImages.DataSource = dt; gvImages.DataBind(); } } } } protected void Upload(object sender, EventArgs e) { string fileName = Path.GetFileName(filemailerupl.PostedFile.FileName); string filePath = &#34;~/mailers/&#34; + DateTime.Now.ToString(&#34;ddMMyyyy_HH_mm_ss&#34;) + &#34;_&#34; + fileName; filemailerupl.SaveAs(Server.MapPath(filePath)); string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { string sql = &#34;INSERT INTO tblFilespath VALUES(@Name, @Path)&#34;; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue(&#34;@Name&#34;, fileName); cmd.Parameters.AddWithValue(&#34;@Path&#34;, filePath); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } Response.Redirect(Request.Url.AbsoluteUri); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using conn As SqlConnection = New SqlConnection(constr) Using sda As SqlDataAdapter = New SqlDataAdapter(&#34;SELECT * FROM tblFilespath&#34;, conn) Dim dt As DataTable = New DataTable() sda.Fill(dt) gvImages.DataSource = dt gvImages.DataBind() End Using End Using End If End Sub Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs) Dim fileName As String = Path.GetFileName(filemailerupl.PostedFile.FileName) Dim filePath As String = &#34;~/mailers/&#34; &amp; DateTime.Now.ToString(&#34;ddMMyyyy_HH_mm_ss&#34;) &amp; &#34;_&#34; &amp; fileName filemailerupl.SaveAs(Server.MapPath(filePath)) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using conn As SqlConnection = New SqlConnection(constr) Dim sql As String = &#34;INSERT INTO tblFilespath VALUES(@Name, @Path)&#34; Using cmd As SqlCommand = New SqlCommand(sql, conn) cmd.Parameters.AddWithValue(&#34;@Name&#34;, fileName) cmd.Parameters.AddWithValue(&#34;@Path&#34;, filePath) conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using Response.Redirect(Request.Url.AbsoluteUri) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/zc67iqz.gif" alt="" width="288" height="277" /></p>https://www.aspforums.net:443/Threads/902553/Insert-Save-unique-file-in-Folder-Directory-and-Path-in-Database-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/902553/Insert-Save-unique-file-in-Folder-Directory-and-Path-in-Database-using-C-and-VBNet-in-ASPNet/Thu, 06 Jun 2019 01:29:57 GMT