I have a site that I've almost completed and while testing it today noticed that it's not uploading any files or doing the bulk insert in SQL that I need it to. Here is my codebehind:
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Submit1_Click(ByVal sender As Object, ByVal e As EventArgs)
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 = Server.MapPath("Data") & "\" & fn
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write(<center>Thank you for your submission.</center>)
' get the file that was submitted and check if it was .txt file
Dim theFile As FileInfo = New FileInfo(SaveLocation)
If theFile.Extension <> ".txt" Then
Response.Write(<center>Please submit a text file.</center>)
End If
Dim importPath As String = Server.MapPath("Data") & "\upload.txt"
If File.Exists(importPath) Then
' do something with existing upload.txt file, maybe archive?
End If
' rename the uploaded file to upload.txt for importing
theFile.MoveTo(importPath)
' and bulk import the data:
Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
Dim results As New DataTable
Using con As New SqlConnection(connection)
con.Open()
' execute the bulk import
Using cmd As SqlCommand = con.CreateCommand
cmd.CommandText = "bulk insert dialerresults from '" & importPath & "' " & _
" with ( fieldterminator = ',', rowterminator = '\n' )"
cmd.ExecuteNonQuery()
End Using
End Using
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
End Try
Else
Response.Write(<center>Please select a file to upload.</center>)
End If
End Sub
End Class
Can anyone see why the file is not being uploaded? I'm sure its something simple but I've been working on this for a while and may have missed something. Any assistance would be welcome.
Thank you,
Doug
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Submit1_Click(ByVal sender As Object, ByVal e As EventArgs)
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 = Server.MapPath("Data") & "\" & fn
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write(<center>Thank you for your submission.</center>)
' get the file that was submitted and check if it was .txt file
Dim theFile As FileInfo = New FileInfo(SaveLocation)
If theFile.Extension <> ".txt" Then
Response.Write(<center>Please submit a text file.</center>)
End If
Dim importPath As String = Server.MapPath("Data") & "\upload.txt"
If File.Exists(importPath) Then
' do something with existing upload.txt file, maybe archive?
End If
' rename the uploaded file to upload.txt for importing
theFile.MoveTo(importPath)
' and bulk import the data:
Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
Dim results As New DataTable
Using con As New SqlConnection(connection)
con.Open()
' execute the bulk import
Using cmd As SqlCommand = con.CreateCommand
cmd.CommandText = "bulk insert dialerresults from '" & importPath & "' " & _
" with ( fieldterminator = ',', rowterminator = '\n' )"
cmd.ExecuteNonQuery()
End Using
End Using
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
End Try
Else
Response.Write(<center>Please select a file to upload.</center>)
End If
End Sub
End Class
Can anyone see why the file is not being uploaded? I'm sure its something simple but I've been working on this for a while and may have missed something. Any assistance would be welcome.
Thank you,
Doug