Click here to Skip to main content
15,876,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 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.


ASP.NET
<pre><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" />


ASP.NET
<pre>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 have tried:

What I am expecting replace the existing file to edited/ updated file with same filename. Please help me modify and guide me in my issue. Thank you
Posted
Comments
mtoha 8-May-23 4:54am    
I would recommend, you use temporary file to save original excel. Then doing modification using excel library, and then save modified excel.
Dave Kreskowiak 8-May-23 8:22am    
You CANNOT use Excel Interop in an ASP.NET app. It is not supported in a multi-user environment.

The good thing is none of the code you posted uses Excel Interop. You're using the SAS library to work with Excel, which uses the ACE engine to read data from an Excel sheet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900