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