Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

dougancil

IS-IT--Management
Mar 31, 2009
44
US
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
\ 78

Code:
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Protected Sub Submit1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit1.Click
        Dim SaveLocation As String = Server.MapPath("Data") & "\upload.txt'"
        If UploadFile(SaveLocation) Then
            'the file was uploaded: now try saving it to the database
            SaveToDatabase(SaveLocation)
        End If
    End Sub
    Private Function UploadFile(ByVal SavePath As String) As Boolean
        Dim fileWasUploaded As Boolean = False 'indicates whether or not the file was uploaded

        'Checking if the file upload control contains a file
        If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
            Try
                'checking if it was .txt file BEFORE UPLOADING IT!
                'You used to upload it first...but the file could be a virus
                If File1.FileName.EndsWith(".txt") = False Then
                    'The file is not the expected type...do not upload it
                    'just post the validation message
                    message.Text = "Please submit a text file."
                Else
                    'The file is a .txt file
                    'checking to see if the file exists already
                    'If it does exist Deleting the existing one so that the new one can be created
                    If IO.File.Exists(SavePath) Then
                        IO.File.Delete(SavePath)
                    End If

                    'Now upload the file (save it to your server)
                    File1.PostedFile.SaveAs(SavePath)

                    'After saving it check to see if it exists
                    If File.Exists(SavePath) Then
                        'Upload was sucessful
                        message.Text = "Thank you for your submission"
                        fileWasUploaded = True
                    Else
                        'the file was not saved
                        message.Text = "Unable to save the file"
                    End If
                End If

            Catch Exc As Exception
                'We encountered a problem
                message.Text = Exc.Message + " " + Exc.StackTrace
            End Try
        Else
            'No file was selected for uploading
            message.Text = "Please select a file to upload"
        End If
        Return fileWasUploaded
    End Function

    Private Sub SaveToDatabase(ByVal SavePath As String)
        Try
            ' and bulk import the data:   
            'If ConfigurationManager.ConnectionStrings("Dialerresults") IsNot Nothing Then
            'Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
            Dim connection As String = "data source=10.2.1.40;initial catalog=IVRDialer;uid=sa;password=chili123;"
            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 '" & SavePath & "' " & _
                    "with ( fieldterminator = ',', rowterminator = '\n' )"

                    cmd.ExecuteNonQuery()
                End Using
            End Using
            'Else
            'message.Text="ConfigurationManager.ConnectionStrings('Dialerresults') is Nothing!"
            'End If
        Catch ex As Exception
            message.Text = ex.Message + ex.StackTrace
        End Try
    End Sub

End Class

Here is the line in question:

cmd.ExecuteNonQuery()

Is this not written in the correct syntax?

Thank you,

Doug
 
I would also recommend using parameterized queries. they
1. are easier to read
2. allow the database to optimize the query plans
3. prevent sql injection attacks

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Mark,

The SQL that you see there:

bulk insert dialerresults from '" & SavePath & "' " & _
"with ( fieldterminator = ',', rowterminator = '\n' )"

Is the only SQL that I have (besides the connection string)

If you strip out the SavePath statement, the command is simply:

bulk insert dialerresults
from 'a saved file path'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

So I'm not sure what I'm not seeing. That's all I'm trying to do at this point.
 
Jbenson,

I'm new to debugging and really don't know how to do that. I know that I can insert breakpoints, but I'm sure that's not what you're telling me to do here.
 
Ok I inserted a break point at that line. I looked at the output of the code and I looked at the output of the breakpoint. What I see is that where it says "condition" that it says no condition and then at hit count it says break always (currently 0).
 
Press the green play ">" button on top fo the visual studio window. This will start the applicatin in debug mode and will stop at the bretkpoint.
 
I set the breakpoint and looked through all of the tabs under the breakpoint and don't see any errors.

Here's what I see in my call stack:

App_Web_vt3jqd7q.dll!_Default.SaveToDatabase(String SavePath = "L:\ Line 75 Basic

> App_Web_vt3jqd7q.dll!_Default.Submit1_Click(Object sender = {Text = "Upload"}, System.EventArgs e = {System.EventArgs}) Line 13 + 0xe bytes Basic
 
You have to learn and get used to using the debugger.
ONce the program stops on the line wher you set your break point, check the value fo the cmd.command text in the wach window at the bottom of the screen.(click the watch tab)
 
I've looked in the watch tab and there's no data there.
 
once the program hits the breakpoint, type cmd.commandtext in the name column
 
I'm not able to type anything in the name column. I put the breakpoint on this line of code:

cmd.ExecuteNonQuery()

and when I open the watch window, I am unable to type anything into the name column.
 
he already posted the problem:

Code:
App_Web_vt3jqd7q.dll!_Default.SaveToDatabase(String SavePath = "L:\[URL unfurl="true"]wwwroot\Webfile1\Data\upload.txt'")[/URL] Line 75    Basic

there is a quote(') after upload.txt that doesn't belong there...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top