Upload and replace existing file in Database using C# and VB.Net in ASP.Net

Last Reply 19 days ago By pandeyism

Posted 20 days ago

Hi

i want to replace existing file in db from fileupload(multiple files) .

i have to replace file which is in DB from front end using fileupload.

user has to upload 2 files one is .html and another is .xml from frontend. In front end ther are 2 dropdownlist.

Based on id that will come from dropdownlist selection in the textbox the upload file has get replaced.

i need to replace files in particular fields (vcFilePath and Rawdata) in the coloumn based on ID. using 2 fileupload is also fine with me.

        protected void Upload_Click(object sender, EventArgs e)
        {
            foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles)
            {
                string filename = Path.GetFileName(postedFile.FileName);
                string filename1 = Path.GetFileName(postedFile.FileName);
                string iModuleId = TextBox2.Text;
                //string contentType = postedFile.ContentType;
                using (Stream fs = postedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        //byte[] bytes1 = br.ReadBytes((Int32)fs.Length);

                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
                        //SqlCommand cmd = new SqlCommand("update tblBookNodes_AMM set @vcFilePath,@Rawdata where (@iModuleId='" + TextBox2.Text + "')");
                        string query = "update tblBookNodes_AMM set @vcFilePath='"+FileUpload2.FileName.ToString()+"',@Rawdata='"+FileUpload2.FileName.ToString()+"'";
                        query += "where iModuleId='" + TextBox2.Text+"'";
                        using(SqlCommand cmd=new SqlCommand(query))
                        {
                            cmd.Connection = conn;
                            cmd.Parameters.AddWithValue("@vcFilePath", bytes);
                            cmd.Parameters.AddWithValue("@Rawdata", bytes);
                            //cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text);
                            conn.Open();
                            cmd.ExecuteNonQuery();
                            conn.Close();
                        }
                    }
                }
            }
        }

 

Posted 19 days ago Modified on 19 days ago

Hi suhaas121,

Refer below code.

Namespaces

C#

using System.IO;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.IO
Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

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() == ".html")
        {
            vcBytes = bytes;
        }
        if (fileExtension.ToLower() == ".xml")
        {
            rawBytes = bytes;
        }
    }
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    string query = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId";
    using (SqlCommand cmd = new SqlCommand(query))
    {
        cmd.Connection = conn;
        cmd.Parameters.AddWithValue("@vcFilePath", vcBytes);
        cmd.Parameters.AddWithValue("@Rawdata", rawBytes);
        cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

 VB.Net

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() = ".html" Then
            vcBytes = bytes
        End If

        If fileExtension.ToLower() = ".xml" Then
            rawBytes = bytes
        End If
    Next
    Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
    Dim query As String = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId"
    Using cmd As SqlCommand = New SqlCommand(query)
        cmd.Connection = conn
        cmd.Parameters.AddWithValue("@vcFilePath", vcBytes)
        cmd.Parameters.AddWithValue("@Rawdata", rawBytes)
        cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
    End Using
End Sub