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.
<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" />
<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