I'm building a program that imports data from an Excel file into a database, or by writing into textboxes (by an insert in asp.net) or both, but I want to give a warning if the user tries to enter the same record that is already in database (duplicated).
And I would like to do that by coding or setting something in SQL, to be easier to the user...
Here's my table:
Mov. Date     Value Date    description of the movement     Value in EUROS
---------------------------------------------------------------------------
12-12-2001    12-12-2001    DEPOSITO EM NUMERARIO 222       200,01
12-12-2001    12-12-2001    DEPOSITO EM NUMERARIO 223       200,01
12-12-2001    12-12-2001    DEPOSITO EM NUMERARIO 224       200,02
if the user enters a record equal to any of these, it says a warning and the user can try again
Here's the code (asp.net C#):
protected void Upload_Click(object sender, EventArgs e)
{
    string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
    string filename = Path.GetFileName(filepath);
    FileUpload1.SaveAs(excelPath);
    string ext = Path.GetExtension(filename);
    string strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
    OleDbCommand cmd = new OleDbCommand("Select * from [rptListaMovs_4$A15:D75]", excelConnection);
    excelConnection.Open();
    cmd.ExecuteNonQuery();
    OleDbDataReader dReader;
    dReader = cmd.ExecuteReader();
    DataTable dtFail = new DataTable();
    using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
    {
        sqlBulk.ColumnMappings.Add("Mov. Date", "Mov. Date");
        sqlBulk.ColumnMappings.Add("Value Date", "Value Date");
        sqlBulk.ColumnMappings.Add("description of the movement", "description of the movement");
        sqlBulk.ColumnMappings.Add("Value in EUROS", "Value in EUROS");
        sqlBulk.DestinationTableName = "Dados";
        var temp = dtFail.AsEnumerable().Distinct();
        dtFail = temp.CopyToDataTable();
        sqlBulk.WriteToServer(dReader);
    }
    excelConnection.Close();
}
 
    