Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,

I have a asp.net web application where i'm trying to upload & read the excel file.

But frequently i'm facing this error:
The process cannot access the file 'C:\gis.xlsx' because it is being used by another process.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: The process cannot access the file 'C:\gis.xlsx' because it is being used by another process.

Source Error: 


Line 43: 
Line 44:                 string FilePath = Server.MapPath(FolderPath + FileName);
Line 45:                 FileUpload1.SaveAs(FilePath);
Line 46: 
Line 47:                 Repeater1.DataSource = ImportExcel.Import_To_Grid(FilePath, Extension, "Yes", rbHDR.SelectedItem.Text);

Source File: C:\Users\Developer\documents\visual studio 2010\Projects\GIS\GIS\ImportGIS.aspx.cs    Line: 45 


c# code
if (FileUpload1.HasFile)
            {
                string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                string FolderPath = ConfigurationManager.AppSettings["FolderPath"];

                string FilePath = Server.MapPath(FolderPath + FileName);
                FileUpload1.SaveAs(FilePath);//Error

                Repeater1.DataSource = ImportExcel.Import_To_Grid(FilePath, Extension, "Yes", rbHDR.SelectedItem.Text);
                Repeater1.DataBind();

                FileUpload1.Dispose();
            }


importExcel.cs
public static System.Data.DataTable Import_To_Grid(string FilePath, string Extension, string isHDR, string SheetName)
        {
            string conStr="";
            switch (Extension)
            {
                case ".xls": //Excel 97-03
                    conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                    break;
                case ".xlsx": //Excel 07
                    conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                    break;
            }
            conStr = String.Format(conStr, FilePath, isHDR);
            OleDbConnection connExcel = new OleDbConnection(conStr);
            OleDbCommand cmdExcel = new OleDbCommand();
            OleDbDataAdapter oda = new OleDbDataAdapter();
            System.Data.DataTable dt = new System.Data.DataTable(); 
            cmdExcel.Connection = connExcel;

            //Get the name of First Sheet
            connExcel.Open();
            System.Data.DataTable dtExcelSchema;
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            //string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
            connExcel.Close();

            //Read Data from First Sheet
            connExcel.Open();
            cmdExcel.CommandText = "SELECT modetrans, name, regno, grade, division, fatname, nameofcomp, TRIM(faph) AS faph, TRIM(moph) AS moph, TRIM(compph) AS compph, TRIM(resiph) AS resiph, TRIM(resiph1) AS resiph1, nationality, sex  From [" + SheetName + "$]";
            oda.SelectCommand = cmdExcel;
            oda.Fill(dt);
            connExcel.Close(); 
        
            return dt;
        }


please, can any one help me...

Thanks
Posted
Updated 15-Nov-14 21:04pm
v6
Comments
Afzaal Ahmad Zeeshan 16-Nov-14 2:49am    
Do you create it on the runtime?

Anyways the chances are that you're using the file somewhere, mostly when you use the resources it is a better approach to use them in using block.
MT_ 16-Nov-14 3:00am    
What exactly are you doing in ImportExcel.Import_To_Grid? can you show that?
abdul subhan mohammed 16-Nov-14 3:04am    
i have added .cs code
abdul subhan mohammed 16-Nov-14 3:02am    
still i'm getting the same error

Error is because the fileupload or any control or object still connected to the Excel file..

You need to dispos and use "Using" block for avoiding the object still used afer the function..

Also the excel file should be open by "Open" Exclusive mode.. can set this property

You can try to open the file in read-only mode:


var app = new Microsoft.Office.Interop.Excel.Application();
var workbook = app.Workbooks.Open(filename, ReadOnly: true);

Or you can try to save it in shared mode:


workbook.SaveAs(filename, AccessMode: XlSaveAsAccessMode.xlShared);

Thanks,
ullas
 
Share this answer
 
Try this, see if it helps...

C#
public static System.Data.DataTable Import_To_Grid(string FilePath, string Extension, string isHDR, string SheetName)
{
try
{

// your complete code here
}
catch
{

}
finally
{
    if(cmdExcel !=null)
    {
        cmdExcel.Dispose();
        cmdExcel = null;
    }
    if(connExcel != null)
    {
        connExcel.Dispose();
        connExcel = null;
    }
}

}
 
Share this answer
 
As Error Says You are accessing Resource(File,Disk,etc) which is already used by Another process.Like in Most Situation you need to close/Dispose its objects Streams,Connections.

You should close try catch finally in method Import_To_Grid and Close or Dispose Streams or Connection inside Catch Block

You like this:
Each time to create new File

C#
string FilePath = Server.MapPath(FolderPath + FileName+DateTimeNow.ToString());


C#
public static System.Data.DataTable Import_To_Grid(string FilePath, string Extension, string isHDR, string SheetName)
        {

try
{

using( OleDbConnection connExcel = new OleDbConnection(conStr);)
{
using(OleDbCommand cmd=new OleDbCommand(connExcel,"mySQL"))
{

//Read Your Excel Here
}

}


return dt;


}

catch(Exception e)
{
connExcel.Dispose();
cmd.Dispose();

return null;
}
Finally
{
connExcel.Dispose();
cmd.Dispose();

}
     }
 
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