Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:09:29+00:00 2026-06-02T21:09:29+00:00

I have a problem trying to parameterize some dynamic SQL build in an existing

  • 0

I have a problem trying to parameterize some “dynamic” SQL build in an existing C# class used by an ASP app. The environment is:

  • Win Server 2008
  • .NET 3.0
  • C#
  • DB2 9.x ([IBM][CLI Driver][DB2])

The existing code just concatenates the SQL with the param strings in a long SQL string – which is of course at risk for SQL injection. As is my practice whenever I see this, I tend to change the code to use parameters. But with this code I am failing. I have tried “@” and I have tried “?” – the latter is what I understand to be necessary for ODBC.

Here is a simplified code snippet (forgive me if I don’t format it right – this is my first question) that I have compiled and run:

private DataSet test(String schemaName )
{
        String sortField = "TABLE_NAME.COLUMN_NAME";
        String sortDirection = "ASC";
        OdbcConnection conn = new OdbcConnection();
        DataSet ds = new DataSet();

        string connStr = ConfigurationManager.AppSettings[schemaName] + dbUser;
        try
        {
            conn.ConnectionString = connStr;
            OdbcCommand cmd = new OdbcCommand("SELECT * FROM TABLE_NAME ORDER BY ? ? ");
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(sortField);
            cmd.Parameters.Add(sortDirection);                              
            logger.log("cmd SQL = \t" + cmd.CommandText );

            OdbcDataAdapter da = new OdbcDataAdapter(cmd);
            da.Fill(ds);

            return ds;
        }
        catch (Exception ex)
        {
            ex.Data.Add("Location:", "test()");
            ex.Data.Add("Connection", conn.ConnectionString);
            logger.logException(ex);
            throw ex;
        }
        finally
        {
            conn.Close();
        }
    }

Log printout:

cmd SQL = SELECT * FROM TABLE_NAME ORDER BY ? ?

Where TABLE_NAME is of course the table I am querying.

What I get in return is this (some proprietary info removed:

EXCEPTION occured at 4/26/2012 12:29:41 PM ERROR [42601] [IBM][CLI
Driver][DB2] SQL0104N An unexpected token “?” was found following “”.
Expected tokens may include: “MICROSECONDS MICROSECOND SECONDS SECOND
MINUTES MINUTE HOURS”. SQLSTATE=42601
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)Connection Driver={IBM DB2 ODBC DRIVER};
…..

Changing this to a stored proc is not allowed.

Upgrading to a later version of .NET is not allowed.

Changing/upgrading the ODBC driver is not allowed.

What I am seeing indicates to me that the “?” parameter is not being replaced.

I have tried AddWithValue() and I have tried Add(OdbcType.VarChar).Value = sortField (or something to that effect).

I am kind of at my whit’s end – all of the googling and searching here indicates to me that the code above should work, but so far I have not been able to get the parameters in the SQL substituted with the variables.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:09:30+00:00Added an answer on June 2, 2026 at 9:09 pm

    The reason the ? is an unexpected token is because you are using it in the ORDER BY clause (which I don’t think is allowed).

    The reason to use parameters is to mitigate the risks of user input. When building your query, if the ORDER BY field and direction are not coming via user input, you are safe in building the query with concatenation.

    Only use the ? in the WHERE clause:

    OdbcCommand cmd = new OdbcCommand("SELECT * FROM TABLE_NAME WHERE ID = ? ORDER BY " + sortField + " " + sortDirection);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem trying to understand when building a rails app with several
I have a problem trying to design some generic storage.. Basically I have the
I have a problem trying to unpickle subclasses of this class. When I unpickle
I have this problem when trying to read a session in another asp.net page.
I have a problem trying to stream audio using the AsyncPlayer class in android.
I have a problem trying to show some images on my winform. On one
I have some weird problem with templates. When trying to pass a parameterised iterator
I have a problem trying to get some code that returns unique answers to
I have a problem trying to edit. I work with Areas for better management
I have a problem trying to hide .php extension from the url I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.