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.Upload and replace existing file in Database using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="suhaas121">suhaas121</a>,</p> <p>Refer below code.</p> <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.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Data.SqlClient Imports System.Configuration</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Upload_Click(object sender, EventArgs e) { byte[] vcBytes = null, rawBytes = null; foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles) { string filename = Path.GetFileName(postedFile.FileName); string fileExtension = Path.GetExtension(postedFile.FileName); byte[] bytes = null; using (Stream fs = postedFile.InputStream) { using (BinaryReader br = new BinaryReader(fs)) { bytes = br.ReadBytes((Int32)fs.Length); } } if (fileExtension.ToLower() == &#34;.html&#34;) { vcBytes = bytes; } if (fileExtension.ToLower() == &#34;.xml&#34;) { rawBytes = bytes; } } SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;connString&#34;].ConnectionString); string query = &#34;update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId&#34;; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = conn; cmd.Parameters.AddWithValue(&#34;@vcFilePath&#34;, vcBytes); cmd.Parameters.AddWithValue(&#34;@Rawdata&#34;, rawBytes); cmd.Parameters.AddWithValue(&#34;@iModuleId&#34;, TextBox2.Text); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } }</pre> <p><strong>&nbsp;VB.Net</strong></p> <pre class="brush: vb">Protected Sub Upload_Click(ByVal sender As Object, ByVal e As EventArgs) Dim vcBytes As Byte() = Nothing, rawBytes As Byte() = Nothing For Each postedFile As HttpPostedFile In FileUpload2.PostedFiles Dim filename As String = Path.GetFileName(postedFile.FileName) Dim fileExtension As String = Path.GetExtension(postedFile.FileName) Dim bytes As Byte() = Nothing Using fs As Stream = postedFile.InputStream Using br As BinaryReader = New BinaryReader(fs) bytes = br.ReadBytes(CType(fs.Length, Int32)) End Using End Using If fileExtension.ToLower() = &#34;.html&#34; Then vcBytes = bytes End If If fileExtension.ToLower() = &#34;.xml&#34; Then rawBytes = bytes End If Next Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;connString&#34;).ConnectionString) Dim query As String = &#34;update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId&#34; Using cmd As SqlCommand = New SqlCommand(query) cmd.Connection = conn cmd.Parameters.AddWithValue(&#34;@vcFilePath&#34;, vcBytes) cmd.Parameters.AddWithValue(&#34;@Rawdata&#34;, rawBytes) cmd.Parameters.AddWithValue(&#34;@iModuleId&#34;, TextBox2.Text) conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Sub</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/583062/Upload-and-replace-existing-file-in-Database-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/583062/Upload-and-replace-existing-file-in-Database-using-C-and-VBNet-in-ASPNet/Tue, 06 Aug 2019 01:59:24 GMT