0

Working with upload function, using FileUpload Control and I'm trying to cleanse some data to avoid the duplication by using replace/ update the existing file by scanning the datetag, dateuntag, tag and tagtype to replace/ update the entire row data. I used Interop Excel and manage to upload the excel and save it to the sql serve however I'm having an issue replacing the existing file to the updated data file with same filename (just reupload file to update the changes with same filename). This issue is what happens is the system save a duplication and triggers to a messy database.

ASPX

<asp:FileUpload ID="FileUpload1" onchange="startUI()" runat="server" CssClass="textEntry" Width="250px" />
                            <asp:Button ID="btnUpload" runat="server" style="visibility:hidden;display:none" CssClass="buttons"
                                OnClick="Button1_Click" Text="Upload file" Width="93px" />

ASPX.CS

protected void Button1_Click(object sender, EventArgs e)
{
    if (!this.FileUpload1.HasFile)
    {
        alert("Please select file for uploads.");
        return;
    }
    uploadfile1();
    CHANGEPASSWORD.Class.JSclass.refreshMainPage();
}
public void alert(String msg)
{
    try
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "<script language='javascript'> window.alert('" + msg.Replace("\n", "\\n").Replace("'", "\\'") + "'); </script>", false);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}
void checkDirectory()
{

    if (!System.IO.Directory.Exists(Server.MapPath("") + "\\Uploads"))
    {
        CHANGEPASSWORD.Class.Logger.writeLog(Session["userid"].ToString(), Session["sessionID"].ToString(),
            "frmUploadFile.checkDirectory", "created directory Upload", "", "", Class.LogCategories.SELECT);
        System.IO.Directory.CreateDirectory(Server.MapPath("") + "\\Uploads");
    }
}

private bool uploadfile1()
{
    try
    {
        if (FileUpload1.PostedFile.FileName != "" || FileUpload1.PostedFile.FileName != "File Template")
        {
            this.checkDirectory();
            FileUpload1.PostedFile.SaveAs(Server.MapPath("") + "\\Uploads\\" + FileUpload1.FileName);
            filename = FileUpload1.FileName;
            path = Server.MapPath("") + "\\Uploads\\";

            ExcelCS xl = new ExcelCS();
            xl.UserId = (Session["userid"] != null) ? Session["userid"].ToString() : String.Empty;
            xl.Session = (Session["sessionID"] != null) ? Session["sessionID"].ToString() : String.Empty;

            if (xl.ParseExcelFile(path + filename, true, 1))
            {
                SAVEDATALIST save = new SAVEDATALIST();
                if (save.save())
                {
                    Class.JSclass.ShowAlert(save.RecordsUploaded.ToString() + " item(s) uploaded and " + save.TotalUpdated.ToString() + " item(s) updated out of " + save.TotalRecords.ToString() + " Record(s).");
                    CHANGEPASSWORD.Class.Logger.writeLog(Session["userid"].ToString(), Session["sessionID"].ToString(),
                        "frmUploadFile.uploadfile1", "File uploaded successfully!", "", "", Class.LogCategories.SELECT);
                    Class.JSclass.refreshMainPage();
                    return true;
                }
            }
            else
            {

                Class.JSclass.ShowAlert("Invalid file format please use specified template.");
                CHANGEPASSWORD.Class.Logger.writeLog(Session["userid"].ToString(), Session["sessionID"].ToString(),
                    "frmUploadFile.uploadfile1.ParseExcelFile", "Error: " + xl.Message, "", "", Class.LogCategories.SELECT);
                return false;
            }
        }
    }
    catch (Exception e)
    {
        Class.JSclass.ShowAlert("Error: " + e.Message);
        CHANGEPASSWORD.Class.Logger.writeLog(Session["userid"].ToString(), Session["sessionID"].ToString(),
            "frmUploadFile.uploadfile1", "Error: " + e.Message, "", "", Class.LogCategories.SELECT);
    }
    Class.JSclass.ShowAlert("File upload failed!");
    return false;
}

What I am expecting replace the exixsting file to edited/ updated file with same filename. Please help me modify and guide me in my issue. Thank you

code_ricx
  • 1
  • 2
  • Welcome, what's the issue again? ("I'm having an issue replacing the existing file to updated data file with same filename" ) - do you want to update existing Excel file data or replace it with a new file with upto date data or do you want to save the updated file as something else? – bonCodigo May 08 '23 at 08:01
  • Hi @bonCodigo, the issue is I wanted to replace the existing(old) file to updated file with the same file name, what I meant is the old file that has been uploaded already wanted to reupload to update the changes – code_ricx May 08 '23 at 08:20
  • 1) why don't you try `Streawriter` class? e.g. [like this](https://stackoverflow.com/a/44236226/1389394) 2) and [here](https://stackoverflow.com/a/11434825/1389394) and [here](https://stackoverflow.com/q/44235689/1389394) – bonCodigo May 08 '23 at 10:22
  • but how do you even know what the old file name was, and worse yet, how do you even know the file name belonged to that one user? It not really much clear if you should care about some old file name up-loaded 3 days ago? It would seem that you allow the user to up-load a file, and then process that Excel data to the sql server database. But, unless you save the file name based on some "id" or save to a database table of files uploaded by the ONE user, then how are you going to know about some file the user uploaded 5 days ago, mixed in a folder with files uploaded by other users? – Albert D. Kallal May 09 '23 at 01:55
  • So, you have 2 issues. The first issue is a file up-load. I doubt that you should care, nor bother, or worry about previous file(s) uploaded and processed. You ONLY care to save that file - probably best to give file name some internal userID + date + time type of file name. You then open that Excel file, process the rows to the database. It is at the row processing time you will check/test for duplicate values/rows or whatever you want. once that file been processed, you really don't care nor need it anymore. So, the row add and process routine has to do this checking. – Albert D. Kallal May 09 '23 at 01:59
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 09 '23 at 08:57

0 Answers0