0

I need to prevent importing duplicating values from excel sheet. here is my code. this works fine. but I need to improve this code to prevent duplication. need to fix this quickly.

 private void btnGet_Click(object sender, EventArgs e)
                {
                    try
                    {
                        intMode = 1;
                        dsMain = new DataSet();
                        if (dgGrid.Columns.Count != 0)
                        {
                            for (int i = dgGrid.Columns.Count; i > 0; i--)
                            {
                                dgGrid.Columns.Remove(dgGrid.Columns[0].Name);
                            }
                        }
                        dgGrid.Columns.Clear();

                        odfExcelGet.Title = "Excel Upload";
                        odfExcelGet.FileName = "Excel";
                        odfExcelGet.Filter = "Excel File (*.xls;*.xlsx;)|*.xls;*.xlsx;";
                        odfExcelGet.InitialDirectory = @"c:\";
                        odfExcelGet.ShowDialog();
                        txtAddress.Text = odfExcelGet.FileName;

                        this.Cursor = Cursors.WaitCursor; 

                        string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
                        ConnectionString += odfExcelGet.FileName;
                        ConnectionString += @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";";

                        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", ConnectionString);
                        da.Fill(dsMain);

                        dgGrid.DataSource = dsMain.Tables[0];
        //rest of code

                }
Didu
  • 79
  • 2
  • 11
  • 2
    https://stackoverflow.com/questions/4415519/best-way-to-remove-duplicate-entries-from-a-data-table – Riv Oct 30 '17 at 12:51
  • Particularly note the answers following the accepted answer in the above link. – PaulF Oct 30 '17 at 12:55

0 Answers0