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);
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":
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx":
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;
connExcel.Open();
System.Data.DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
connExcel.Close();
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