Results 1 to 11 of 11

Thread: How to fix this syntax error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    How to fix this syntax error

    vb.net Code:
    1. Dim sqlqry2 As String = "SELECT * FROM RSSFeeds ORDER BY NewsID DESC LIMIT " & RSSReturnValue
    2.             Dim conn2 As Data.OleDb.OleDbConnection = New Data.OleDb.OleDbConnection(My.Settings.DatabaseConnectionString)
    3.             Dim cmd2 As Data.OleDb.OleDbCommand = New Data.OleDb.OleDbCommand(sqlqry2, conn2)
    4.             conn2.Open()

    My error is "Syntax error in ORDER BY clause."

    mysql2 = "SELECT * FROM RSSFeeds ORDER BY NewsID DESC LIMIT 150"


    It works on my webserver but not VB how come?

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to fix this syntax error

    I think that's mySQL syntax, and I doubt it would work with any other database.
    Are you connecting to MySQL?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to fix this syntax error

    Its not VB thats telling you its a syntax error its your database provider. Are you referencing the same database?

    I'm not a database expert but I've never seen "LIMIT" before - I've always used "TOP", ie

    "SELECT TOP 150 * FROM RSSFeeds ORDER BY NewsID DESC"

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to fix this syntax error

    @paul the OP would appear to be using MySQL as the database backend, and the SQL syntax is different between SQL Server and MySQL. You can see the LIMIT keyword being used here:

    http://dev.mysql.com/doc/refman/5.1/en/select.html

    @kiwis can you confirm the actual SQL statement that is sent to the server? i.e. what is the value of RSSReturnValue is?

    We will need some more information before anyone will be able to help you.

    If you are actually connecting to MySQL, you might want to think about using the managed provider for it, you can find a link to it in my signature.

    Gary

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to fix this syntax error

    Ah there you go - my SQL experience is all from either Microsoft (SQL Server, Access) or Ingres - "proper" databases

    My initial thought was that the OP might be using two different databases, ie the website is accessing MySQL but the desktop app is accessing a SQL Server database, after all if the syntax of a query is acceptible for a given database from one application I wouldn't have throught that it should fail on the same database connected from another application (even if they are using different libraries for connecting?)

  6. #6
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: How to fix this syntax error

    hey kiwis

    if its true what @keystone_paul is saying that u r using two different database then you may choose following approach i have used this in one of my project when some clients told us to provide mysql support along with mssql

    you may use conditional compilation for it


    vb Code:
    1. 'on top of the class or module
    2. #if MySql
    3. using MySql.Data;
    4. using MySql.Data.MySqlClient;
    5. #else
    6. using System.Data;
    7. using System.Data.SqlClient;
    8. #endif


    vb Code:
    1. ' where you want to use
    2. #if MySql
    3.             MySqlConnection oConn = new MySqlConnection(sMySqlConnectionString);
    4.             MySqlDataAdapter oAdp = new MySqlDataAdapter();
    5. #else
    6.             SqlConnection oConn = new SqlConnection(sSqlConnectionString);
    7.             SqlDataAdapter oAdp = new SqlDataAdapter();
    8. #endif
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to fix this syntax error

    Hey,

    In this situation, the use of conditional compiliation is not really justified, in my opinion,

    If, and we don't know if this is the case yet, the OP wants to support multiple databases, then really they should implement a provider architecture using generic database factory objects rather than database specific ones, where the database being used is set in the configuration file for the application.

    In addition, the code that you have posted is C#, but this is the VB.Net Forum.

    Thanks

    Gary

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to fix this syntax error

    just as gep said, I would avoid using conditional compilation. Instead a configuration in the configuration file should do the work better.
    Last edited by Pradeep1210; May 1st, 2009 at 08:19 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: How to fix this syntax error

    @Gary , sorry for this actually it was from one of my project which is in c#

    now this is in vb.net

    vb Code:
    1. #If MySql Then
    2. Imports MySql.Data
    3. Imports MySql.Data.MySqlClient
    4. #Else
    5. Imports MySql.Data
    6. Imports System.Data.SqlClient
    7. #End If

    vb Code:
    1. #If MySql Then
    2.             Dim oConn As New MySqlConnection(sMySqlConnectionString)
    3.             Dim oAdp As New MySqlDataAdapter()
    4. #Else
    5.             Dim oConn As New SqlConnection(sSqlConnectionString)
    6.             Dim oAdp As New SqlDataAdapter()
    7. #End If
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to fix this syntax error

    OK... why do we think the OP wants to support multiple databases (for which there are better ways than using conditional compiling)...

    At any rate, If memory serves, the LIMIT construct takes two parameters, a starting index and a rows count:
    LIMIT 0,150 .... start with the first record and return 150 rows...

    also, if yo uare connecting to MySQL, you''ll want to get the MySQL .NET Connector, install it, then use the MySQLClient namespace and not OLEDB... which might be part of the problem, as LIMIT isn't part of the SQL standard (yet).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to fix this syntax error

    Hey,

    For clarity:

    With one argument, the value specifies the number of rows to return from the beginning of the result set:

    SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
    In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
    Gary

Posting Permissions

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



Click Here to Expand Forum to Full Width