Code Ricx

Code Ricx

  • 1.6k
  • 14
  • 486

Check if the Excel entries has duplicate in upload asp.net webform

May 3 2023 8:52 AM

I'm trying to work with uploading excel in asp.net c# web form and save in sql however it also upload duplicate data in row.

I tried only tried to save the upload in sql however with duplicate/ same data in upload/save in sql. I wanted to void the duplication of the data

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))
            {
                listdata save = new listdata();
                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).");
                    listweb.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.");
                listweb.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);
        listweb.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;
}

Answers (4)