473,569 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

347 Contributor
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.Sql Client.SqlConne ction.OnError(S qlException exception, Boolean breakConnection ) at System.Data.Sql Client.SqlInter nalConnection.O nError(SqlExcep tion exception, Boolean breakConnection ) at System.Data.Sql Client.TdsParse r.ThrowExceptio nAndWarning(Tds ParserStateObje ct stateObj) at System.Data.Sql Client.TdsParse r.Run(RunBehavi or runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleR esultSet bulkCopyHandler , TdsParserStateO bject stateObj) at System.Data.Sql Client.SqlComma nd.RunExecuteNo nQueryTds(Strin g methodName, Boolean async) at System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult result, String methodName, Boolean sendToPipe) at System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery() at _Default.SaveTo Database(String SavePath) in C:\Inetpub\wwwr oot\Webfile1\De fault.aspx.vb:l ine 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.ExecuteNonQ uery()

Is this not the correcy syntax?
May 11 '10 #1
43 8528
dougancil
347 Contributor
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.Sql Client.SqlConne ction.OnError(S qlException exception, Boolean breakConnection ) at System.Data.Sql Client.SqlInter nalConnection.O nError(SqlExcep tion exception, Boolean breakConnection ) at System.Data.Sql Client.TdsParse r.ThrowExceptio nAndWarning(Tds ParserStateObje ct stateObj) at System.Data.Sql Client.TdsParse r.Run(RunBehavi or runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleR esultSet bulkCopyHandler , TdsParserStateO bject stateObj) at System.Data.Sql Client.SqlComma nd.RunExecuteNo nQueryTds(Strin g methodName, Boolean async) at System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult result, String methodName, Boolean sendToPipe) at System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery() at _Default.SaveTo Database(String SavePath) in C:\Inetpub\wwwr oot\Webfile1\De fault.aspx.vb:l ine 78

The line of code in question is this one:

cmd.ExecuteNonQ uery()

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 Recognized Expert Specialist
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 Recognized Expert Moderator Expert
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 Contributor
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 Recognized Expert Moderator Expert
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 Contributor
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 Recognized Expert Contributor
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 Contributor
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.Sql Client.SqlConne ction.OnError(S qlException exception, Boolean breakConnection ) at System.Data.Sql Client.SqlInter nalConnection.O nError(SqlExcep tion exception, Boolean breakConnection ) at System.Data.Sql Client.TdsParse r.ThrowExceptio nAndWarning(Tds ParserStateObje ct stateObj) at System.Data.Sql Client.TdsParse r.Run(RunBehavi or runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleR esultSet bulkCopyHandler , TdsParserStateO bject stateObj) at System.Data.Sql Client.SqlComma nd.RunExecuteNo nQueryTds(Strin g methodName, Boolean async) at System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult result, String methodName, Boolean sendToPipe) at System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery() at _Default.SaveTo Database(String SavePath) in C:\Inetpub\wwwr oot\Webfile1\De fault.aspx.vb:l ine 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 Recognized Expert Contributor
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

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

Similar topics

4
9893
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
7092
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 syntax near the keyword 'DEFAULT'" The form has 2 fields on it, called tb_username and tb_password. (see code snippet below).
0
1300
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 around this problem? Thanks. Iham
1
11224
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'." Explaining you the scene is the following Stored Proc.
6
2973
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, objDataAdapter.Fill(objDataSet, "mindata") any help is greatly appriciated.
1
2335
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 assignment of "SELECT T..." to the variable query, the last line produces the error: Line 1: Incorrect syntax near '<'. Many thanks in advance for any...
0
8294
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 records there is an error "Incorrect syntax near '-'. Must declare the scalar variable "@UserName". I worked out in design view,code is automatically...
10
3401
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 the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException:...
5
4490
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 and returns a string Data. Using the debugger, I see that the value of ID is correct. Is this a C# problem, or SQL Server 7 problem ?
1
6236
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. Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'. Source Error: Line 35: ...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.