Error: The process cannot access the file it si being used by anot


G

Guest

I am just starting developing with .Net(Have used asp for years). I have
created a very simple upload page which once uploaded opens the file(excel)
and displays the data. If I Attempt to re-run using the same file I get the
message 'Error: The process cannot access the file because it is being used
by another process. '

VS 2005

Here is my code, any suggestions...

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

Private Sub Page_Load()
If Not IsPostBack Then Response.Write("Please Select a File.")
End Sub


Private Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim i As Integer, max As Integer
If Not File1.PostedFile Is Nothing And
File1.PostedFile.ContentLength > 0 Then
Dim fn As String =
System.IO.Path.GetFileName(File1.PostedFile.FileName)
Dim SaveLocation As String = ("c:\inetpub\wwwroot\asp\Data\") & fn
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write("The file has been uploaded.<br>")
'now get the data from the excel file and put in the db
Dim strConn As String = "Provider=Microsoft.Jet.OleDb.4.0;" _
& "data source=c:\inetpub\wwwroot\asp\data\" & fn
& ";" _
& "Extended Properties=Excel 8.0;"
Dim objConn As New OleDbConnection(strConn)
Dim strSql As String = "Select * From Prices"
Dim objCmd As New OleDbCommand(strSql, objConn)
objConn.Open()
Dim objReader As OleDbDataReader
objReader = objCmd.ExecuteReader()
If objReader.HasRows Then
'get field count
max = objReader.FieldCount
'output headings

'getvalues
While objReader.Read()
For i = 0 To max - 1
Response.Write(objReader(i).ToString & "<br>")
Next
End While
'clean up
objReader = Nothing
objCmd = Nothing
objConn = Nothing
Else
Response.Write("NO Data Found.")
End If
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
End Try
Else
Response.Write("Please select a file to upload.")
End If
File1 = Nothing
End Sub
</script>
<html>
<head>
</head>
<body>
<form id="Form1" method="post" enctype="multipart/form-data"
runat="server">
<input id="File1" type="file" name="File1" runat="server" />
<br />
<input id="Submit1" type="submit" value="Upload" runat="server"
onserverclick="Submit1_ServerClick" />
</form>
</body>
</html>
<script language=vbscript>
function form1_onsubmit()
if right(form1.file1.value,4)<>".xls" then alert "Excel Files
Only.":form1_onsubmit=false
end function
</script>
 
Ad

Advertisements

S

Steven Cheng[MSFT]

Hi Adeturner,

Welcome to the ASP.NET newsgroup.

As for the "Error: The process cannot access the file because it is being
used by another process. " error, it is likely caused by the uploaded excel
file be locked by the ASP.NET application process. From the code snippet
you provided, you used the below code to clear the datareader and
connection after finishing the data reading operation:

================
'clean up
objReader = Nothing
objCmd = Nothing
objConn = Nothing
=================

, however, these statements only make those data objects ready for garbage
collection rather than actually release the underlying data connection. For
"DataReader" and "Connection" object of ADO.NET, we should always call the
".Close()" function of them after we finish using them. Therefore, you can
try adding the Close statements for the "OleDbDataReader" and
OleDbConnection in your page code to see whether it helps.

If there're any other problems, please also feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top