Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Delete Uploaded .xls file on DB Server ?????

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-11 : 17:39:18
I'm trying to do the following:
1. Allow user to Upload a .xls file from their machine
2. Take the rows in this .xls file and insert every row into a table on the DB server

I can select this one file (exceltest.xls) that I've been using to upload, and it saves the .xls file records to the DB table and the records are getting built. THIS IS GOOD! I've placed a PK in the table, so I can see the records increasing w/o question.

Problems though are:

If I update this exceltest.xls file with new records, the new records aren't getting updated to the db server table. For example, I first had the three rows of "First", "Second", "Third" in the .xls file. When I add a 4th and 5th record of "Fourth" and "Fifth", those don't show in the DB table.

Now, if I try another .xls file (basically I copied and renamed the first one to exceltest1.xls), I get:

Line 82 Error Updating Table: OLE DB provider 'Microsoft.Jet.OLEDB.4.0' reported an error. [OLE/DB provider returned message: The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.] OLE DB error trace [OLE/DB Provider 'Microsoft.Jet.OLEDB.4.0' IColumnsInfo::GetColumnsInfo returned 0x80004005: ].

The code is:

Dim getmyFile As HttpPostedFile = myfile.PostedFile
Label2.Text = "Line 76"
If IsNothing(getmyFile) Then
Label2.Text = "Please select a file to upload"
Label2.Text = "Line 78"
Else
If getmyFile.ContentLength = 0 Then
Label2.Text = "Cannot upload zero length File"
Else
Dim ServerFileName As String = Path.GetFileName(myfile.PostedFile.FileName)
Server.MapPath("//dbserver")
getmyFile.SaveAs("C:\TestSaving\" & ServerFileName)
Label2.Text = "Successful upload to C:\TestSaving\" & ServerFileName
sCon1.Open()
Dim strSQL As String
Dim err As String
strSQL = "Insert into ExcelImport Select * FROM OPENROWSET"
strSQL &= "('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\TestSaving\" & ServerFileName & ";"""
strSQL &= "HDR = YES ','SELECT * FROM [Sheet1$]')"
Label3.Text = strSQL.ToString()
Dim cmd As New SqlCommand(strSQL, sCon1)
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
Label2.Text = "Line 82 Error Updating Table: "
Label2.Text &= ex.Message
Finally
sCon1.Close()
End Try
End If
End If

My question is do I need to delete this file after it's been uploaded and the records have been built to the table?

I do have a folder on dbserver that's shared: C:\TestSaving

Thanks for the help. I'm almost there!
   

- Advertisement -