473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Line 1: Incorrect syntax near ','. Unclosed quotation mark before the character ...

347 100+
Can someone please review my code and see where I can be missing information. I get the following error:

Line 1: Incorrect syntax near ','. Unclosed quotation mark before the character string ' )'. at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQuer yTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.SaveToDatabase(String SavePath) in C:\Inetpub\wwwroot\Webfile1\Default.aspx.vb:line 78


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) Handles Submit1.Click
  10.         Dim SaveLocation As String = Server.MapPath("Data") & "\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 = "Please submit a text file."
  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.             ' and bulk import the data:   
  64.             'If ConfigurationManager.ConnectionStrings("Dialerresults") IsNot Nothing Then
  65.             'Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
  66.             Dim connection As String = "data source=10.2.1.40;initial catalog=IVRDialer;uid=sa;password=xxx;"
  67.             Dim results As New DataTable
  68.  
  69.             Using con As New SqlConnection(connection)
  70.                 con.Open()
  71.  
  72.                 ' execute the bulk import   
  73.                 Using cmd As SqlCommand = con.CreateCommand
  74.  
  75.                     cmd.CommandText = "bulk insert dialerresults from '" & SavePath & "' " & _
  76.                     "with ( fieldterminator = ',', rowterminator = '\n' )"
  77.  
  78.                     cmd.ExecuteNonQuery()
  79.                 End Using
  80.             End Using
  81.             'Else
  82.             'message.Text="ConfigurationManager.ConnectionStrings('Dialerresults') is Nothing!"
  83.             'End If
  84.         Catch ex As Exception
  85.             message.Text = ex.Message + ex.StackTrace
  86.         End Try
  87.     End Sub
  88.  
  89. End Class
Here is the line of code in question:

cmd.ExecuteNonQuery()

Is this not the correcy syntax?
May 11 '10 #1
43 8478
dougancil
347 100+
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) Handles Submit1.Click
  10.         Dim SaveLocation As String = Server.MapPath("Data") & "\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 = "Please submit a text file."
  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.             ' and bulk import the data:   
  64.             'If ConfigurationManager.ConnectionStrings("Dialerresults") IsNot Nothing Then
  65.             'Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
  66.             Dim connection As String = "data source=10.2.1.40;initial catalog=IVRDialer;uid=sa;password=xxx;"
  67.             Dim results As New DataTable
  68.  
  69.             Using con As New SqlConnection(connection)
  70.                 con.Open()
  71.  
  72.                 ' execute the bulk import   
  73.                 Using cmd As SqlCommand = con.CreateCommand
  74.  
  75.                     cmd.CommandText = "bulk insert dialerresults from '" & SavePath & "' " & _
  76.                     "with ( fieldterminator = ',', rowterminator = '\n' )"
  77.  
  78.                     cmd.ExecuteNonQuery()
  79.                 End Using
  80.             End Using
  81.             'Else
  82.             'message.Text="ConfigurationManager.ConnectionStrings('Dialerresults') is Nothing!"
  83.             'End If
  84.         Catch ex As Exception
  85.             message.Text = ex.Message + ex.StackTrace
  86.         End Try
  87.     End Sub
  88.  
  89. End Class
  90.  
and when I try to upload to the page that this references I get the following error:

Line 1: Incorrect syntax near ','. Unclosed quotation mark before the character string ' )'. at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQuer yTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.SaveToDatabase(String SavePath) in C:\Inetpub\wwwroot\Webfile1\Default.aspx.vb:line 78

The line of code in question is this one:

cmd.ExecuteNonQuery()

The syntax of the SQL query is correct. I'm not sure what I'm missing but there is something here that I must be missing. Can anyone offer any assistance?
May 11 '10 #2
tlhintoq
3,525 Expert 2GB
Please don't double-post your questions. It divides attempts to help you in an organized and cohesive manner. Your threads have been merged
May 11 '10 #3
Frinavale
9,735 Expert Mod 8TB
I think your problem is on lines 75 and 76 in the above posted code:
Expand|Select|Wrap|Line Numbers
  1.  cmd.CommandText = "bulk insert dialerresults from '" & SavePath & "' " & _
  2.                      "with ( fieldterminator = ',', rowterminator = '\n' )"
See Bulk Insert (Transact-SQL) for help with Bulk Insert.

-Frinny
May 12 '10 #4
dougancil
347 100+
Frinny,

I think that you're right but I have no idea what is causing the error. I understand that sometimes by having a single quotation mark that this is seen as a line break, I've tried manipulating this file in a lot of different ways and I don't see what I have to do to fix this. Can you give me any ideas as to what needs to be done here?

Thank you,

Doug
May 13 '10 #5
Frinavale
9,735 Expert Mod 8TB
Honestly, I've never used this command before.

I would refer to the link I posted for you to come up with an answer for you....if you scroll down there are examples of how use this SQL command. Maybe they can help you?

I'll ask the SQL experts to take a look at this.

-Frinny
May 13 '10 #6
dougancil
347 100+
Frinny,

If you take the sql bulk insert string as just a query, it works fine. I wrote that long before I started this quest. The query itself executes correctly. I'm pretty sure that it may be the syntax on how it's written and inserted into the asp.net code here for this page that is causing this problem. I'm just not sure where. If you could ask the experts, I'd greatly appreciate it.

Doug
May 13 '10 #7
Megalog
378 Expert 256MB
If it's a problem with the single quote being misinterpreted, you can always swap them with a unique character set while building the string, and then do a replace on it afterwards with the double quotes. I've used your post, with carats as the replaced character.

Expand|Select|Wrap|Line Numbers
  1. cmd.CommandText = Replace("bulk insert dialerresults from ^" & SavePath & "^ " & _ 
  2.                  "with ( fieldterminator = ^,^, rowterminator = ^\n^ )","^","""")
This will return:

Expand|Select|Wrap|Line Numbers
  1. bulk insert dialerresults from "C:\Folder" with ( fieldterminator = ",", rowterminator = "\n" )
May 13 '10 #8
dougancil
347 100+
I tried with the carat's replacing the single quotes, and still get the same error:

Line 1: Incorrect syntax near '^'. Unclosed quotation mark before the character string '^ with ( fieldterminator = ^,^, rowterminator = ^\n^ )'. at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQuer yTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.SaveToDatabase(String SavePath) in C:\Inetpub\wwwroot\Webfile1\Default.aspx.vb:line 78

here's the line of code after I modified it:

cmd.CommandText = "bulk insert dialerresults from ^" & SavePath & "^ " & _
"with ( fieldterminator = ^,^, rowterminator = ^\n^ )"
May 13 '10 #9
Megalog
378 Expert 256MB
You need to add the replace function to that to swap the carats back to double quotes.

Look at the first code block from my last post.
May 13 '10 #10
dougancil
347 100+
I used the replace and the code now looks as follows:
Expand|Select|Wrap|Line Numbers
  1.      cmd.CommandText = Replace("bulk insert dialerresults from ^" & SavePath & "^ " & _"with ( fieldterminator = ^,^, rowterminator = ^\n^ )", "^", """")
and I get this back:
Expand|Select|Wrap|Line Numbers
  1. Line 1: Incorrect syntax near ','. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.SaveToDatabase(String SavePath) in C:\Inetpub\wwwroot\Webfile1\Default.aspx.vb:line 78
So it does appear to be a problem with the syntax for the query itself. Here's the query as it was originally written:
Expand|Select|Wrap|Line Numbers
  1. bulk insert dialerresults
  2. from '\\MSBWEB3\data\upload.txt'
  3. WITH
  4. (
  5. FIELDTERMINATOR = ',',
  6. ROWTERMINATOR = '\n'
  7. )
That query works if you run it from the SQL query analyzer. So I'm not sure what syntax asp.net needs to see it in for it to work.
May 13 '10 #11
Frinavale
9,735 Expert Mod 8TB
Could you post the contents of your SaveLocation string at run time?
May 13 '10 #12
NeoPa
32,556 Expert Mod 16PB
You have shown the error message in detail, but not the actual resolved string from the Replace() function. I would post that, and even compare it with the string (original command) that you know works well.

See below for some general instructions when working with SQL in code (It was written with VBA in mind but I'm sure it could be made to work for you easily).
One of the most popular (frequently occurring rather than best liked) problems we get is with SQL strings being manipulated in VBA code.

The reason this is so difficult is that all the work is being done at a level of redirection. What I mean by this is that the coder is never working directly with the SQL itself, but rather with code which in turn, is relied on to produce the SQL that they are envisaging is required. It's rather similar to the problems coders have historically had dealing with pointers.

Anyway, a technique I often suggest to coders struggling with this (at any level. This happens to experienced coders too.) is to use either the MsgBox() function, or Debug.Print into the Immediate Pane of the debugger window, to display the value of the SQL in the string before using it (That's assuming you're not good with debugging generally. Personally I would trace to the line, then display the value prior to allowing execution of the string - See Debugging in VBA). It's really much easier to appreciate what a SQL string is meant to do, and where there may be problems, when you can see it in its entirety, and in its true form, rather than as the code is about to create it.
May 13 '10 #13
Frinavale
9,735 Expert Mod 8TB
Just so you know, this is an ASP.NET application and so Message Box and Debug.Print are not a viable option.

Please use an ASP.NET Label or Localize control instead of the Message Box or Debug.Print.

For example:
Expand|Select|Wrap|Line Numbers
  1. debugLabel.Text = SaveLocation
  2. debugLabel.Text = debugLabel.Text +  "<br />" +  Replace("bulk insert dialerresults from ^" & SavePath & "^ " & _"with ( fieldterminator = ^,^, rowterminator = ^\n^ )", "^", """")
Where debugLabel is defined as Label in the ASPX page.

-Frinny
May 13 '10 #14
dougancil
347 100+
Frinny,

So on the aspx page, I should just put a label somewhere on the page and define the debugLabel.Text to it?
May 13 '10 #15
Frinavale
9,735 Expert Mod 8TB
Yup, drag a Label control from the Toolbox onto the ASPX page and give it an ID..."debugLabel".

You'll delete it after you're finished debugging.
May 13 '10 #16
dougancil
347 100+
here's the error message I get back when I insert the debugLabel.Text

ExecuteNonQuery: CommandText property has not been initialized at System.Data.SqlClient.SqlCommand.ValidateCommand(S tring method, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.SaveToDatabase(String SavePath) in C:\Inetpub\wwwroot
\Webfile1\Default.aspx.vb:line 78

also here is the line of debug code from the save path:
Expand|Select|Wrap|Line Numbers
  1. C:\Inetpub\wwwroot\Webfile1\Data\upload.txt'
  2. bulk insert dialerresults from "C:\Inetpub\wwwroot\Webfile1\Data\upload.txt'" with ( fieldterminator = ",", rowterminator = "\n" )
May 13 '10 #17
Frinavale
9,735 Expert Mod 8TB
Hmm I think you have an extra/unnecessary '
In your SavePath...See it? It's at the end...
C:\Inetpub\wwwroot\Webfile1\Data\upload.txt'
May 13 '10 #18
dougancil
347 100+
I took that extra quotation mark out and now get this error:

ExecuteNonQuery: CommandText property has not been initialized at System.Data.SqlClient.SqlCommand.ValidateCommand(S tring method, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.SaveToDatabase(String SavePath) in C:\Inetpub\wwwroot\Webfile1\Default.aspx.vb:line 78
May 13 '10 #19
Frinavale
9,735 Expert Mod 8TB
I can't guess what your current code is....
Could you please post your most recent code responsible for updating the database.

-Frinny

PS. Please post code in code tags.
May 13 '10 #20
dougancil
347 100+
Frinny,

Here is my current code for that page.

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) Handles Submit1.Click
  10.         Dim SaveLocation As String = Server.MapPath("Data") & "\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 = "Please submit a text file."
  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.             ' and bulk import the data:   
  64.             'If ConfigurationManager.ConnectionStrings("Dialerresults") IsNot Nothing Then
  65.             'Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
  66.             Dim connection As String = "data source=10.2.1.40;initial catalog=IVRDialer;uid=sa;password=xxx;"
  67.             Dim results As New DataTable
  68.  
  69.             Using con As New SqlConnection(connection)
  70.                 con.Open()
  71.  
  72.                 ' execute the bulk import   
  73.                 Using cmd As SqlCommand = con.CreateCommand
  74.  
  75.                     DebugLabel.Text = SavePath
  76.                     DebugLabel.Text = DebugLabel.Text + "<br />" + Replace("bulk insert dialerresults from ^" & SavePath & " " & "with ( fieldterminator = ^,^, rowterminator = ^\n^ )", "^", """")
  77.  
  78.                     cmd.ExecuteNonQuery()
  79.                 End Using
  80.             End Using
  81.             'Else
  82.             'message.Text="ConfigurationManager.ConnectionStrings('Dialerresults') is Nothing!"
  83.             'End If
  84.         Catch ex As Exception
  85.             message.Text = ex.Message + ex.StackTrace
  86.         End Try
  87.     End Sub
  88.  
  89. End Class
May 13 '10 #21
Frinavale
9,735 Expert Mod 8TB
Dougancil,
You are not supplying a SQL command to the SqlCommand Object you are using to connect to the database.

Declare a string that is going to contain your SQL command text:
Expand|Select|Wrap|Line Numbers
  1. Dim sqlQueryText As String = _
  2.   "BULK INSERT dialerresults " + _
  3.   "FROM '"& SavePath & "' " + _
  4.   "WITH ( FIELDTERMINATOR = ',' , ROWTERMINATOR = '\n' )"
  5.  
You need to make sure that there aren't any extra apostrophes (') that shouldn't be there....like I was mentioning before. I'm not sure what you did to "fix" the problem before...but anyways.

The other thing you must do...which you have actually been told by the exception details...is provide the SQL command Text to the SqlCommand object you're using to connect to the database...see the code below:
Expand|Select|Wrap|Line Numbers
  1.   Try
  2.     Dim sqlQueryText As String = _
  3.       "BULK INSERT dialerresults " + _
  4.       "FROM '"& SavePath & "' " + _
  5.       "WITH ( FIELDTERMINATOR = ',' , ROWTERMINATOR = '\n' )"
  6.  
  7.  
  8.     ' and bulk import the data:   
  9.     'If ConfigurationManager.ConnectionStrings("Dialerresults") IsNot Nothing Then
  10.     'Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
  11.     Dim connection As String = "data source=10.2.1.40;initial catalog=IVRDialer;uid=sa;password=xxx;"
  12.  
  13. 'I removed the DataTable declaration because you're not using it.
  14.     Using con As New SqlConnection(connection)
  15.       con.Open()
  16.       ' execute the bulk import   
  17.         Using cmd As New  SqlCommand(sqlQueryText, con)
  18.           cmd.ExecuteNonQuery()
  19.         End Using
  20.       End Using
  21.  
  22.     'Else
  23.       'message.Text="ConfigurationManager.ConnectionStrings('Dialerresults') is Nothing!"
  24.     'End If
  25.     Catch ex As Exception
  26.       message.Text = sqlQueryText + "<br />"+ ex.Message + "<br />" + ex.StackTrace
  27.     End Try
  28.  
See the MSDN documentation on the SqlCommand class for a more detailed explanation of how to use the SqlCommand.
May 14 '10 #22
dougancil
347 100+
Frinny,

Ok so I'm now back to this error:

Could not bulk insert because file 'C:\Inetpub\wwwroot\Webfile1\Data\upload.txt' could not be opened. Operating system error code 3(The system cannot find the path specified.). at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQuer yTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.SaveToDatabase(String SavePath) in C:\Inetpub\wwwroot\Webfile1\Default.aspx.vb:line 82

I've modified the machine.config to impersonate the admin for the web server. Evidently that's not what I need to do. The Default Application Pool for that server is logged in as a Network Service, but I was under the impression that by impersonating the admin in the machine.config file that it overwrote how the asp.net page would log in on that server. Is that not correct?
May 14 '10 #23
Frinavale
9,735 Expert Mod 8TB
The error is:
"Operating system error code 3(The system cannot find the path specified.)"

Are you sure that this is the correct path?
C:\Inetpub\wwwroot\Webfile1\Data\upload.txt

-Frinny
May 14 '10 #24
Frinavale
9,735 Expert Mod 8TB
Hey!
Line 10:
Expand|Select|Wrap|Line Numbers
  1. Dim SaveLocation As String = Server.MapPath("Data") & "\upload.txt'"
See the extra apostrophe?

Change line 10 to:
Expand|Select|Wrap|Line Numbers
  1. Dim SaveLocation As String = Server.MapPath("Data") & "\upload.txt"
May 14 '10 #25
dougancil
347 100+
I'm absolutely sure.
May 14 '10 #26
dougancil
347 100+
I removed that, sorry. That's not there anymore.
May 14 '10 #27
dougancil
347 100+
here's what I put in my system.web portion of machine.config and I know that it's being used because if I put

identity impersonate="true" that it stops working

<system.web>
<processModel enable="true"
userName="MSB\administrator"
password="xxxxxxx"/>
May 14 '10 #28
Frinavale
9,735 Expert Mod 8TB
You probably should not be modifying the system.config nor the machine.config files....

Like I said, I've never used the bulk insert stuff before...I have no way of testing it so I can't tell you what I find either.


How about you try creating a test application that is a Win Forms or a Console type of application. Try doing a bulk insert from there.

These applicatoins run under your Windows User account and so if you as a user have access to the file your application should also have access to the file.


-Frinny
May 14 '10 #29
dougancil
347 100+
Frinny,

As this is a "sandbox" server ... (since I only want this page to run correctly) I have no fear in modifying the machine.config file. My question would be if it's not that, then what is it? How can I modify what user that the asp.net application tries to log in as? Because evidently that's what's causing the issue here.
May 14 '10 #30
Frinavale
9,735 Expert Mod 8TB
Are you Sure that's what's causing the issue?
I still recommend you try the Win Forms or Console application....it won't take long...just copy paste the bulk insert code and manually supply the path (instead of using Server.MapPath)
May 14 '10 #31
dougancil
347 100+
Frinny,

I'm just going by the power of deductive reasoning here. I can see that the file is being uploaded to the correct folder on the server. The SQL query is written correctly, so to me all of the other mechanics are worked out. If it's not a permissions issue with opening the file, I'm not sure what else it could be.
May 14 '10 #32
Frinavale
9,735 Expert Mod 8TB
Ok I went back to the Bulk Insert article I posted you a link to earlier.

I found a few things:
MSDN BULK INSERT (Transact-SQL) Article:
data_file must specify a valid path from the server on which SQL Server is running. If data_file is a remote file, specify the Universal Naming Convention (UNC) name. A UNC name has the form \\Systemname\ShareName\Path\FileName. For example, \\SystemX\DiskZ\Sales\update.txt.
And this bit is about permissions required to be set up with the SQL Server:
MSDN BULK INSERT (Transact-SQL) Article:
<Bulk Insert> Requires INSERT and ADMINISTER BULK OPERATIONS permissions. Additionally, ALTER TABLE permission is required if one or more of the following is true:

* Constraints exist and the CHECK_CONSTRAINTS option is not specified.
ms188365.note(en-us,SQL.100).gifNote:
Disabling constraints is the default behavior. To check constraints explicitly, use the CHECK_CONSTRAINTS option.

* Triggers exist and the FIRE_TRIGGER option is not specified.
ms188365.note(en-us,SQL.100).gifNote:
By default, triggers are not fired. To fire triggers explicitly, use the FIRE_TRIGGER option.

* You use the KEEPIDENTITY option to import identity value from data file.
-Frinny
May 14 '10 #33
dougancil
347 100+
Frinny,

I've tried the UNC and by mapping the path as follows:

\\MSBWEB3\wwwroot\Webfile1\Data and MSBWEB3\\wwwroot\Webfile1\Data\upload.txt and received the following error:

Could not find a part of the path 'C:\Inetpub\wwwroot\Webfile1\MSBWeb3\wwwroot\Webfi le1\Data\MSBWeb3\wwwroot\Webfile1\Data\upload.txt'

When I press upload without the UNC, the file does get uploaded to the correct folder, so that's not the issue. Secondly, when I run the bulk insert command from the sql query analyzer, this bulk insert works fine, which tells me that the permissions on the table for bulk insertion is correct, otherwise I wouldn't be able to do that. I'm not using any constraints as this is a pretty simple query as well as I'm not using any triggers or the keep identity. This is really a WYSIWYG in the sense that the file is uploaded, checked to verify it's a particular type of file, sent to a directory on the web server and then bulk inserted to the sql table. That's it. Nothing else goes on behind the scenes and there's no further contraints or conditions behind it.
May 14 '10 #34
Frinavale
9,735 Expert Mod 8TB
So your SQL Server is not on the same computer as the Web Server hosting this application?
May 14 '10 #35
dougancil
347 100+
no it's not ... its in my network though.
May 14 '10 #36
Frinavale
9,735 Expert Mod 8TB
The SQL Server cannot open the file because the file is not on the same computer......

Try to Share the folder that you are upload the file to so that both computers on the network can access the file.

This is probably not a good thing to do when you release this application (because security issues allowing people to upload files to a directory that is shared on the network is a pretty scary thing to do)...but for now just try sharing the folder so that both computers can access the file.

You could consider using a different means to copy the file to a location where the SQL Server has access to the file later.

-Frinny

PS you have to use the Universal Naming Convention (UNC) to specify the location of the file in your SQL command text............
May 14 '10 #37
dougancil
347 100+
I have shared the file so that I can open it from both computers. I can open/edit/delete from both. So what about if I saved the file on the SQL server rather than on the web server. Would that work?
May 14 '10 #38
Frinavale
9,735 Expert Mod 8TB
I think so.
May 14 '10 #39
dougancil
347 100+
so then I'd have to modify this path to reflect the sql server path correct?

Dim SaveLocation As String = Server.MapPath("Data") & "\upload.txt"
May 14 '10 #40
Frinavale
9,735 Expert Mod 8TB
Well here's the thing, I'm not sure if the ASPNET user account has privileges to copy files from one computer across the network to another computer...I've never tried to do this before.

So yes, you'll have to save the file to the SQL Server...
You will have to make 2 modifications:

1) to the SaveLocation variable so that the ASP.NET application can upload/save the file to the computer hosting the SQL Server

2) to the SQL command text....specifying the path to the file on the SQL server
May 14 '10 #41
dougancil
347 100+
So let me understand this ... if I want to move files from SQL server to a web server (according to MS) it's best to have both my asp.net architecture as well as my sql server be one in the same machine? This seems really convoluted to do such a "simple" task. I know that all of this can be done with "stored procedures" but the ONLY difference is that I'm not using a sp but rather an actual bulk insert from a web server to a sql server.

I've modified the savelocation variable but the sql command text, this:

Using cmd As New SqlCommand(sqlQueryText, con)

is all I have using the SqlCommand.
May 14 '10 #42
Frinavale
9,735 Expert Mod 8TB
I'm not certain that it is best to have your SQL server and your ASP.NET application on the same computer....but it would make life a lot easier for dealing with files that are used by both applications.

I'm sorry I don't have any straight answers for you because I've never used the BULK INSERT command before.

In this case all I can do is recommend trying a few different things...it may help to have both the SQL Server and the ASP.NET application on the same computer in this case....especially if the ASPNET user account cannot copy files across the network (which I'm not certain of so you'd have to test it)

-Frinny
May 14 '10 #43
NeoPa
32,556 Expert Mod 16PB
dougancil: So let me understand this ... if I want to move files from SQL server to a web server (according to MS) it's best to have both my asp.net architecture as well as my sql server be one in the same machine? This seems really convoluted to do such a "simple" task.
Doug, as far as web servers go, if you think it's convoluted to require services be available natively to the web service, then you possibly need to give some consideration to the whole concept of the world wide web itself.

To allow members of the public to access web servers generally, there must be a logical security basis. Web servers generally, and for good logical reasons, give access only to those local resources that are deemed appropriate. If the account that the web service ran under had network rights then the whole thing would be almost impossible to administer.

It is possible (though can be tricky) to get around these restrictions, but not recommended for anyone who doesn't, or can't, grasp the complexities of the situation overall. I don't believe this has anything to do with any MS specific implementations.
May 14 '10 #44

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

Similar topics

4
by: Carl | last post by:
Can you tell me what is wrong with this syntax ? string select = "UPDATE .. " + "(,,,,,,, ,,,, ,,, , , , ) " + " VALUES (@id,@clientid,@total,@tps,@tvq,@gtotal,@datefac,@datepay,
11
by: Mark Findlay | last post by:
Hello Experts! I am attempting to use the OleDbCommand.ExecuteScaler() function within my ASP.NET C# web page to perform a simple validation, but receive the following error: "Incorrect...
0
by: Iham Sheen | last post by:
Hi, When I click at the Update button to commit changes in a MxDataGrid, I get the error message "Unclosed quotation mark ...." if there is a single quote in any of the columns. How can I work...
1
by: Sandesh | last post by:
Hello All, Me saying " has any body come across such error would be underestimating". Well I am getting a very peculiar and unique error "Line 1: Incorrect syntax near 'Actions'." ...
6
by: martin1 | last post by:
I just use DataSet to bind DataSetGrid and display from SQL DB. when starting run in Visual Studio 2005, get "Line 1: Incorrect syntax near '1'" error message from below fill line, ...
1
by: iporter | last post by:
In the following code, the two Response.Write statements output exactly the same - I can copy and paste both into Query Analyzer, and run them fine. However, if I comment out line 3, the...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
10
by: arial | last post by:
Hi, I am getting this error message: Incorrect syntax near the keyword 'where'. Description: An unhandled exception occurred during the execution of the current web request. Please review...
5
by: =?Utf-8?B?QW5kcmV3?= | last post by:
Hi, What is wrong with this code ? I got this error: " Line 1: Incorrect syntax near 'sp_Collect'. " which appears at the last line when | call the cm.ExecuteScalar . The sp takes an int ID...
1
by: karenkksh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.