locked
Find Duplicate Records In Excel While Uploading into gridView through c sharp RRS feed

  • Question

  • User4354200 posted

    Hi,

        I have Excel sheet and have 1000 records are there, i want to verify duplicate name and id, before updating in gridview, Please Help me.

     

    Thanks,

    Shashi!!

    Tuesday, December 20, 2011 3:44 AM

All replies

  • User541108374 posted

    Hi,

    you might want to check out the LinqToExcell project for this. Linq itself has a distinct function for filtering out the duplicates.

    Grz, Kris.

    Tuesday, December 20, 2011 3:58 AM
  • User1839833660 posted

    Refer Upload and read Excel file In Asp.Net 

    Write this method to find duplicates in datatable 

    public DataTable RemoveDuplicate(DataTable dt, string column)
    {
    Hashtable hash = new Hashtable();
    ArrayList aList = new ArrayList();
    foreach (DataRow row in dt.Rows)
    {
    if (hash.Contains(row[column]))
    aList.Add(row);
    else
    hash.Add(row[column], string.Empty);
    }

    foreach (DataRow Row in aList)
    dt.Rows.Remove(Row);

    return dt;
    }
    
    
    And Change button_Click code to this 
    
    
    protected void btnUpload_Click(object sender, EventArgs e)
    {
    string connectionString ="";
    if (FileUpload1.HasFile)
    {
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
    string fileLocation = Server.MapPath("~/App_Data/" + fileName);
    FileUpload1.SaveAs(fileLocation);

    //Check whether file extension is xls or xslx

    if (fileExtension == ".xls")
    {
    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
    }
    else if (fileExtension == ".xlsx")
    {
    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
    }

    //Create OleDB Connection and OleDb Command

    OleDbConnection con = new OleDbConnection(connectionString);
    OleDbCommand cmd = new OleDbCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.Connection = con;
    OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
    DataTable dtExcelRecords = new DataTable();
    con.Open();
    DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
    cmd.CommandText = "SELECT * FROM [" + getExcelSheetName +"]";
    dAdapter.SelectCommand = cmd;
    dAdapter.Fill(dtExcelRecords);
    dtExcelRecords = RemoveDuplicate(dtExcelRecords, "EmpID");
    con.Close();
    GridView1.DataSource = dtExcelRecords;
    GridView1.DataBind();





    }
    }
    Tuesday, December 20, 2011 4:11 AM
  • User960609757 posted

    Hi,

    check the following urls,

    Upload excel file to sql and check duplicate to update record
    http://forums.asp.net/t/1686084.aspx/1

    How to remove duplicate records or create list of unique records in Excel
    http://support.microsoft.com/kb/262277

    Upload And Read Excel File In Asp.Net
    http://csharpdotnetfreak.blogspot.com/2011/12/upload-and-read-excel-file-in-aspnet.html

    Tuesday, December 20, 2011 9:50 AM
  • User-183940590 posted

    Hashtable hash = new Hashtable();
    ArrayList aList = new ArrayList();
    foreach (DataRow row in dt.Rows)
    {
    if (hash.Contains(row[column]))
    aList.Add(row);
    else
    hash.Add(row[column], string.Empty);
    }

    I found this code useful.
    My problem has been solved by this.

    Thanks amit

    Wednesday, December 28, 2011 8:28 AM