dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY'.'

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Resolved [RESOLVED] [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY'.'

    [ATTACH]169221

    Cant understand pls help
    Thanks for reviewing
    Attached Images Attached Images  

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,887

    Re: [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY

    Welcome to VBForums

    Take a look at the contents of the variable sql

    The value you set after the line with the error will contain "... FROM crewORDER BY ...", but it should have a space in: "... FROM crew ORDER BY ..."

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY

    Name:  1.PNG
Views: 13
Size:  7.5 KB
    This is my data source , change line after error dint fix it

  4. #4
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    724

    Re: [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY

    Don't post code in image attachments.

    You didn't post updated code, so we have no idea if you fixed the issue si pointer out properly or not. That being said, since it was a simple fix, I'm going to assume you did fix it properly.

    It is entirely possible for a SQL statement to have more than one issue, so when you say "didn't fix it", it DID fix that problem that si pointed out if you corrected it properly, but that doesn't mean the SQL statement is valid as there could be other problems.

    That being said, your code is confusing, since above the highlighted line you appear to be making changes to the variable "sql" and we can't see any of the code you use above that to build that variable outside of appending "asc" or "desc" appropriately.

    So it appears your issue lies in code you haven't posted, so until you do so there isn't much more to say.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY

    Code:
      datast = New DataSet
            sql = "SELECT primaryTitle FROM basics TABLE"
            sql &= "ORDER BY primaryTitle"
    
            If Not IsFieldSorted() Then
                sql &= "DESC"
            Else
                sql &= "ASC"
            End If
    
            Dim command As New SqlCommand(sql, Connection)
            [adapter] = New SqlDataAdapter(command)
            [adapter].Fill(datast, "TitleName")
    
            sql = "SELECT directors FROM crew TABLE"
            sql &= "ORDER BY directors"
    
            If IsFieldSorted() Then
                sql &= "ASC"
            Else
                sql &= "DESC"
            End If
    
            command.CommandText = sql
            [adapter].SelectCommand = command
            [adapter].Fill(datast, "directorsname")
    
    
    
    
        End Sub
    This is my coding sorry cause new to this forums

  6. #6
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    724

    Re: [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY

    Ok, well, you didn't correct the issue si pointed out, and you have it twice. When you are building a string like you are, you need to explicitly include any and all spaces that need to exist. So you need to either add a space between the word "TABLE" and before the closing quote mark, or between the opening quote and the word "ORDER". Your choice.

    Edit: You also need to make sure there will be a space between the table name and the sort order type when you append either "ASC" or "DESC" to the end of the sql statement.
    Last edited by OptionBase1; Jun 14th, 2019 at 05:09 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: [Error]System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'BY

    Problems fixed after spacing , thanks for the help. Nice forums and peoples

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width