1
Reply

The process cannot access the file error

Edison Augusthy

Edison Augusthy

Sep 3 2016 12:28 AM
270
i made a code to add multiple excel sheets to add to database.but when am trying to add same file its showing following error-"The process cannot access the file 'C:\Users\Sinnonteq\Desktop\my projects\Import\Import\Content\b.xlsx' because it is being used by another process."(b.xlsx is the excel file i tried).is it because of some type of locking mechanism in asp..? some one told me that with "using" i can solve this problem .since am new to mvc please help if anyone knows..
 
here is the code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.OleDb;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Mvc;  
  8. using System.Xml;  
  9. using System.Data.SqlClient;  
  10. using System.Configuration;  
  11. namespace Import.Controllers  
  12. {  
  13.     public class HomeController : Controller  
  14.     {  
  15.         //  
  16.         // GET: /Home/  
  17.   
  18.         public ActionResult Index()  
  19.         {  
  20.             return View();  
  21.         }  
  22.         [HttpPost]  
  23.         public ActionResult Index(HttpPostedFileBase file)  
  24.         {  
  25.             DataSet ds = new DataSet();  
  26.             if (Request.Files["file"].ContentLength > 0)  
  27.             {  
  28.                 string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName);  
  29.   
  30.                 if (fileExtension == ".xls" || fileExtension == ".xlsx")  
  31.                 {  
  32.                     string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName;  
  33.   
  34.   
  35.                      
  36.   
  37.                     {  
  38.   
  39.                         if (System.IO.File.Exists(fileLocation))  
  40.                         {  
  41.   
  42.                             System.IO.File.Delete(fileLocation);  
  43.                         }  
  44.   
  45.                     }  
  46.   
  47.   
  48.   
  49.                     Request.Files["file"].SaveAs(fileLocation);  
  50.                     string excelConnectionString = string.Empty;  
  51.                     excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";  
  52.                     //connection String for xls file format.  
  53.                     if (fileExtension == ".xls")  
  54.                     {  
  55.                         excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";  
  56.                     }  
  57.                     //connection String for xlsx file format.  
  58.                     else if (fileExtension == ".xlsx")  
  59.                     {  
  60.   
  61.                         excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";  
  62.                     }  
  63.                     //Create Connection to Excel work book and add oledb namespace  
  64.                     OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);  
  65.                     excelConnection.Open();  
  66.                     DataTable dt = new DataTable();  
  67.   
  68.                     dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
  69.                     if (dt == null)  
  70.                     {  
  71.                         return null;  
  72.                     }  
  73.   
  74.                     String[] excelSheets = new String[dt.Rows.Count];  
  75.                     int t = 0;  
  76.                     //excel data saves in temp file here.  
  77.                     foreach (DataRow row in dt.Rows)  
  78.                     {  
  79.                         excelSheets[t] = row["TABLE_NAME"].ToString();  
  80.                         t++;  
  81.                     }  
  82.                     OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);  
  83.   
  84.   
  85.                     string query = string.Format("Select * from [{0}]", excelSheets[0]);  
  86.                     using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))  
  87.                     {  
  88.                         dataAdapter.Fill(ds);  
  89.                     }  
  90.                 }  
  91.                 if (fileExtension.ToString().ToLower().Equals(".xml"))  
  92.                 {  
  93.                     string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;  
  94.                     if (System.IO.File.Exists(fileLocation))  
  95.                     {  
  96.                         System.IO.File.Delete(fileLocation);  
  97.                     }  
  98.   
  99.                     Request.Files["FileUpload"].SaveAs(fileLocation);  
  100.                     XmlTextReader xmlreader = new XmlTextReader(fileLocation);  
  101.                     // DataSet ds = new DataSet();  
  102.                     ds.ReadXml(xmlreader);  
  103.                     xmlreader.Close();  
  104.                 }  
  105.   
  106.                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
  107.                 {  
  108.                     string conn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;  
  109.                     SqlConnection con = new SqlConnection(conn);  
  110.                     string query = "if not exists (select 1 from data where phone = '" + ds.Tables[0].Rows[i][2].ToString() + "')Insert into data(name,addres,phone) Values('" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][2].ToString() + "')";  
  111.                     con.Open();  
  112.                     SqlCommand cmd = new SqlCommand(query, con);  
  113.                     cmd.ExecuteNonQuery();  
  114.                     con.Close();  
  115.                 }  
  116.   
  117.             }  
  118.             return View();  
  119.         }  
  120.     }  
  121.       
  122.     }  
  123.           
 

Answers (1)