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 7012973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:19:32+00:00 2026-05-27T22:19:32+00:00

Short: SQL statement in my C# code is not working. with(nolock) is breaking the

  • 0

Short:

SQL statement in my C# code is not working. with(nolock) is breaking the code.

Detailed:

Below are my errors and the code where I am getting the error. The code is supposed to connect to my SQL Server database (connection code works fine) then run a query. This query will get the ip addresses of all events that have a uri of “blah”. The issue seems to be my with(nolock) command that I am required to use. I have to use it as it’s a group standard for all T-SQL queries.

I googled around for a while but nothing seems to fit my issue and the fixes I found haven’t worked yet. Any help with my code or links would be greatly appreciated.

Error:

System.Data.SqlClient.SqlException was caught Message=Incorrect
syntax near the keyword ‘with’. If this statement is a common table
expression, an xmlnamespaces clause or a change tracking context
clause, the previous statement must be terminated with a semicolon.
Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=15
LineNumber=1 Number=319 Procedure=”” Server= State=1

Code:

try
{
   //create sql reader to display data
   SqlDataReader myReader = null;

   //create string to enter data into database
   string insString = @"select c_ip from @dates with(nolock) where cs_uri like 'blah'";
   SqlCommand myCommand = new SqlCommand(insString, DbConnection);

   //populate and sanitize parameters
   myCommand.Parameters.Add("@dates", SqlDbType.VarChar, 100);
   myCommand.Parameters["@dates"].Value = currentdate;

   //execute the command
   myReader = myCommand.ExecuteReader();

   //read all results and print them to output
   while (myReader.Read())
   {
      //get IPs              
      String ipmix = myReader["c_ip"].ToString();
      mainIPs.Add(ipmix);
   }
}
catch (Exception e)
{
   Console.WriteLine("The query connection to the datebase has timed out.\n");
   Console.WriteLine(e.ToString());
   Console.ReadLine();
}

Solution:

Change code from:

//create string to enter data into database
string insString = @"select c_ip from @dates with(nolock) where cs_uri like 'blah'";

to:

//create string to enter data into database
string insString = @"select c_ip from " + currentdate + " with(nolock) where cs_uri like '%blah'";
  • 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-05-27T22:19:33+00:00Added an answer on May 27, 2026 at 10:19 pm

    Get rid of the parameter code and add the tablename in when you build your select statement

    string insString = @"select c_ip from " + currentdate + " with(nolock) where cs_uri like 'blah'";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short, C# throws me Dynamic SQL generation for the UpdateCommand is not supported
Do T-SQL queries in SQL Server support short-circuiting? For instance, I have a situation
In Short I am thoroughly confused by the array of SQL Server services available,
Long story short is I tried to quickly update a single row in SQL
I'm trying to use linq to sql for my project (very short deadline), and
I have a short question. Im my current project I'm using LINQ-to-SQl. That is
So we have a piece of software which has a poorly written SQL statement
So I'm trying to get some tutorial code working, and I'm stuck. I have
In Short: I'm using VB.NET 2008 to connect to a Visual Foxpro 6 Database
I found some great sql code for dynamic sort & order but I am

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.