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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:17:39+00:00 2026-06-11T11:17:39+00:00

SqlConnection connection = new SqlConnection(connString.ToString()); string select = SELECT (CASE WHEN MAX(page_no) IS NULL

  • 0
 SqlConnection connection = new SqlConnection(connString.ToString());
    string select = "SELECT (CASE WHEN MAX(page_no) IS NULL  THEN 1 ELSE MAX(page_no)+1 END) FROM dbo.BOOK";
    string insert = "INSERT INTO dbo.BOOK (book_id,select) VALUES (121,4)";
    SqlCommand sqlCommand = new SqlCommand(insert,connection);
    insert.ExecuteNonQuery();

Here I got exception where the insert contains invalid string select.
Please tell me how assign sub query within the insert?

  • 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-11T11:17:40+00:00Added an answer on June 11, 2026 at 11:17 am

    you cannot use a select statement like this
    if you want to use a sub query it has to be in single statement

    but in above statement you write in different different statement for selection
    and insert query .

    so cmd.ExecuteNonquery() execute only insert text statement so SQL engine unable to find SELECT(and SELECT is a Reserved keyword) so it gives you a error

    if you go with subquery try this

    SqlConnection connection = new SqlConnection(connString.ToString());
    
    string select = "SELECT 121, (CASE WHEN MAX(page_no) IS NULL  THEN 1 ELSE MAX(page_no)+1 END) FROM dbo.BOOK";
    string insert = "INSERT INTO dbo.BOOK (book_id,[select]) "+select;
    
    SqlCommand sqlCommand = new SqlCommand(insert,connection);
    

    sqlCommand.ExecuteNonQuery();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

string query = select * from cfo_daily_trans_hist; try { using (SqlConnection connection = new
if (validateEmailId(email)) { pictureBox5.Visible = true; SqlConnection con = new SqlConnection(conection string); SqlCommand cmd2
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
variable Itemtext is already declared. Why this error?? Try conn = New SqlConnection(connString) conn.Open()
SqlConnection connection = new SqlConnection(cnnString); try { connection.Open(); SqlCommand command = new SqlCommand([dbo].[tblMessages_Insert], connection);
I've seen this code lately: static List<Thread> list = new List<Thread>(); static void Main(string[]
private void btnCheck_Click(object sender, EventArgs e) { string costring = connection(); string MyQuery =
I'm encountering a problem with my database connection. I started with a new blank
I am trying to retrieve some rows from the database using simple SELECT statement
I like the Add Connection or SQL Connection dialog that is in Visual Studio

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.