Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 22nd, 2009, 02:38 PM
Authorized User
 
Join Date: Jul 2006
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default The process cannot access the file...

I have a simple web form consisting of only a file upload control and submit button that fires the btnUpload_Click method. The file (when the "data" directory is empty) uploads successfully, updates my SQL Server table, and executes the "UpdateTierModel_Delete" stored procedure.

When the "data" directory already has a copy of the file, I get the error "Error: The process cannot access the file 'C:\inetpub\wwwroot\NBGTier\data\NBG Tier Users.xlsx' because it is being used by another process." When I attempt to delete the file manually from Explorer, I get an error that "the action can't be completed because the file is open in IIS Worker Process".

How do I get the IIS Worker Process to release the file after it's been processed so that it may be deleted after it's been processed?

Code:
public partial class _Default : System.Web.UI.Page 
{ 
    string oleConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\inetpub\\wwwroot\\NBGTier\\data\\NBG Tier Users.xlsx';Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'"; 
    string sqlConnString = "Data Source=sql432;Initial Catalog=nbg_t;Persist Security Info=True;User ID=*****;Password=*****"; 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        fuExcelFile.Focus(); 
    } 
    protected System.Data.DataTable GetTableData(string wkSheet) 
    { 
        OleDbConnection cn = new OleDbConnection(oleConnString); 
        OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + wkSheet + "] WHERE F11 = 'Y'", cn); 
        cmd.CommandType = CommandType.Text; 
 
        DataSet ds = new DataSet(); 
        OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
 
        da.Fill(ds, "tablex"); 
        DataRow[] dr = ds.Tables[0].Select(); 
        foreach (DataRow row in ds.Tables[0].Rows) 
        { 
            row["F4"] = row["F4"]; 
            row["F11"] = row["F11"]; 
        } 
        ds.Tables[0].AcceptChanges(); 
 
        return ds.Tables["tablex"]; 
    } 
    public string[] GetExcelSheetNames() 
    { 
        OleDbConnection con = null; 
        System.Data.DataTable dt = null; 
        con = new OleDbConnection(oleConnString); 
        con.Open(); 
        dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
        if (dt == null) 
        { 
            return null; 
        } 
        String[] excelSheetNames = new String[dt.Rows.Count]; 
        int i = 0; 
        foreach (DataRow row in dt.Rows) 
        { 
            excelSheetNames[i] = row["TABLE_NAME"].ToString(); 
            i++; 
        } 
        return excelSheetNames; 
    } 
    protected void btnUpload_Click(object sender, EventArgs e) 
    { 
        if (fuExcelFile.HasFile == false) 
        { 
            Response.Write("Please select a file to upload."); 
        } 
        else 
        { 
            if (fuExcelFile.PostedFile.FileName.Contains("NBG Tier Users.xlsx")) 
            { 
                string fn = System.IO.Path.GetFileName(fuExcelFile.PostedFile.FileName); 
                string SaveLocation = Server.MapPath("data") + "\\" + fn; 
                try 
                { 
                    fuExcelFile.PostedFile.SaveAs(SaveLocation); 
                    Response.Write("The file has been processed."); 
                    String[] sheetNames = GetExcelSheetNames(); 
                    DataView dv = new DataView(GetTableData(sheetNames[0].ToString())); 
 
                    SqlConnection cn = new SqlConnection(sqlConnString); 
                    cn.Open(); 
                    foreach (DataRow row in dv.Table.Select()) 
                    { 
                        SqlCommand cmd = new SqlCommand("INSERT INTO Temp_Tier (AcctNo) VALUES (" + row["F4"].ToString() + ")", cn); 
                        cmd.CommandType = CommandType.Text; 
                        cmd.ExecuteNonQuery(); 
                    } 
                    cn.Close(); 
                    DeleteTempStatements(SaveLocation); 
                } 
                catch (Exception ex) 
                { 
                    Response.Write("Error: " + ex.Message); 
                } 
            } 
            else 
            { 
                Response.Write("The file name must be <b>&quot;NBG Tier Users.xlsx&quot;</b>"); 
            } 
        } 
    } 
    protected void DeleteTempStatements(string fileName) 
    { 
        SqlConnection cn = new SqlConnection(sqlConnString); 
        SqlCommand cmd = new SqlCommand("UpdateTierModel_Delete", cn); 
        cmd.CommandType = CommandType.StoredProcedure; 
 
        cn.Open(); 
        cmd.ExecuteNonQuery(); 
        cn.Close(); 
 
        System.IO.File.Delete(fileName); 
    } 
}

Last edited by Earl Hickey; December 22nd, 2009 at 02:41 PM.. Reason: format
 
Old December 22nd, 2009, 02:59 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You probably need to Close and Dispose your OleDbConnection object in GetExcelSheetNames. Or better yet, wrap your code in a using block:

using (OleDbConnection con = new OleDbConnection(oleConnString))
{
...
}

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Earl Hickey (December 22nd, 2009)
 
Old December 22nd, 2009, 03:03 PM
Authorized User
 
Join Date: Jul 2006
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default

It seems I always miss the obvious. Thanks a bunch!





Similar Threads
Thread Thread Starter Forum Replies Last Post
The process cannot access the file... DolphinBay ASP.NET 1.0 and 1.1 Professional 9 May 23rd, 2014 04:38 AM
Access is denied in Process.GetProces(.....) Manoj Bisht ASP.NET 3.5 Professionals 5 March 31st, 2009 02:11 AM
The process cannot access the file nelly78 .NET Framework 2.0 3 July 22nd, 2008 06:59 AM
The process cannot access the file while publishin madhu15583 ASP.NET 1.0 and 1.1 Professional 0 June 19th, 2008 01:58 AM
file error ...being used by another process rreynolds Pro VB.NET 2002/2003 6 January 11th, 2005 11:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.