Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have uploaded an excel file but when once again when i upload the prevoius file does not get deleted.It occurs as "File already exists"

My code is as follows:
------------------------------------------------
If File.Exists("c:\craupload\NLAO_Upload.xls") Then
           
           If conn.State <> ConnectionState.Open Then
               conn.Open()
           End If
           strDelFile = "exec master..xp_cmdshell 'del     c:\craupload\NLAO_Upload.xls'"
           cmd = New SqlCommand(strDelFile, conn)
           cmd.CommandTimeout = 0
           intFileDelete = cmd.ExecuteNonQuery
           If conn.State <> ConnectionState.Closed Then
               conn.Close()
           End If
       End If

--------------------------------------------
But this does not Deletes the file as soon as the data is inserted in temp table.Please Suggest!!!!!!!!

Thanks In Advance
Posted
Updated 1-Oct-10 1:59am
v3
Comments
Henry Minute 1-Oct-10 8:04am    
Does it throw an exception? Any error message? I would have thought that it would have thrown one.

1 solution

The following lines of code will do the trick:

C#
private void Form1_Load(object sender, EventArgs e)
{
      DataTable dtExcel = GetDataFromExcel();

     /*Implement logic to insert rows in database*/
       System.IO.File.Delete("C:\\my\\Book1.xls");

}
public DataTable GetDataFromExcel()
{
            DataTable dt = new DataTable();
            con.ConnectionString = connectionString;
            cmd.Connection = con;
            OleDbDataAdapter dap = new OleDbDataAdapter();
            dap.SelectCommand = cmd;
            dap.Fill(dt);
            return dt;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900