By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,602 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,602 IT Pros & Developers. It's quick & easy.

Data file uploaded but bulk insert isnt working

100+
P: 347
I have the following code:

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Partial Class _Default
  5.     Inherits System.Web.UI.Page
  6.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  7.  
  8.     End Sub
  9.     Protected Sub Submit1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  10.         Dim SaveLocation = "\\MSBWEB3\wwwroot\Webfile1\Reminders\Doug_Ancil\upload.txt"
  11.         If UploadFile(SaveLocation) Then
  12.             'the file was uploaded: now try saving it to the database
  13.             SaveToDatabase(SaveLocation)
  14.         End If
  15.     End Sub
  16.     Private Function UploadFile(ByVal SavePath As String) As Boolean
  17.         Dim fileWasUploaded As Boolean = False 'indicates whether or not the file was uploaded
  18.  
  19.         'Checking if the file upload control contains a file
  20.         If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
  21.             Try
  22.                 'checking if it was .txt file BEFORE UPLOADING IT!
  23.                 'You used to upload it first...but the file could be a virus
  24.                 If File1.FileName.EndsWith(".txt") = False Then
  25.                     'The file is not the expected type...do not upload it
  26.                     'just post the validation message
  27.                     message.Text = "The file you submitted is not a .txt file, please select a .txt file to upload."
  28.                 Else
  29.                     'The file is a .txt file
  30.                     'checking to see if the file exists already
  31.                     'If it does exist Deleting the existing one so that the new one can be created
  32.                     If IO.File.Exists(SavePath) Then
  33.                         IO.File.Delete(SavePath)
  34.                     End If
  35.  
  36.                     'Now upload the file (save it to your server)
  37.                     File1.PostedFile.SaveAs(SavePath)
  38.  
  39.                     'After saving it check to see if it exists
  40.                     If File.Exists(SavePath) Then
  41.                         'Upload was sucessful
  42.                         message.Text = "Thank you for your submission"
  43.                         fileWasUploaded = True
  44.                     Else
  45.                         'the file was not saved
  46.                         message.Text = "Unable to save the file"
  47.                     End If
  48.                 End If
  49.  
  50.             Catch Exc As Exception
  51.                 'We encountered a problem
  52.                 message.Text = Exc.Message + " " + Exc.StackTrace
  53.             End Try
  54.         Else
  55.             'No file was selected for uploading
  56.             message.Text = "Please select a file to upload"
  57.         End If
  58.         Return fileWasUploaded
  59.     End Function
  60.  
  61.     Private Sub SaveToDatabase(ByVal SavePath As String)
  62.         Try
  63.             Dim sqlQueryText As String = _
  64.               "BULK INSERT dialerresults " + _
  65.               "FROM '" & SavePath & "' " + _
  66.               "WITH ( FIELDTERMINATOR = ',' , ROWTERMINATOR = '\n' )"
  67.  
  68.  
  69.             ' and bulk import the data:   
  70.             'If ConfigurationManager.ConnectionStrings("Dialerresults") IsNot Nothing Then
  71.             'Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
  72.             Dim connection As String = "data source=10.2.1.40;initial catalog=IVRDialer;uid=xxx;password=xxxx;"
  73.  
  74.             Using con As New SqlConnection(connection)
  75.                 con.Open()
  76.                 ' execute the bulk import   
  77.                 Using cmd As New SqlCommand(sqlQueryText, con)
  78.  
  79.                     cmd.ExecuteNonQuery()
  80.  
  81.                 End Using
  82.             End Using
  83.  
  84.             'Else
  85.             'message.Text="ConfigurationManager.ConnectionStrings('Dialerresults') is Nothing!"
  86.             'End If
  87.         Catch ex As Exception
  88.             message.Text = ex.Message + " " + ex.StackTrace
  89.  
  90.         End Try
  91.     End Sub
  92.  
  93. End Class
  94.  
What's happening is when I try to upload a file, it tells me that everything is fine but when I look in my database, there is no data there. What am I missing? This worked before but we had to move this to another directory on the server and after we did that, the bulk insert function seems to be broken, though the server isn't returning any kinds of errors.

Any help would be appreciated.

Thank you,

Doug
Jun 28 '10 #1
Share this Question
Share on Google+
1 Reply


100+
P: 347
@dougancil
This seems to be working now. I have no idea why it wasnt working but it seems to be just fine.

Thank you

Doug
Jun 28 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.