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

  • Home
  • SEARCH
  • 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 104585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:18:17+00:00 2026-05-11T01:18:17+00:00

I have a query to insert a row into a table, which has a

  • 0

I have a query to insert a row into a table, which has a field called ID, which is populated using an AUTO_INCREMENT on the column. I need to get this value for the next bit of functionality, but when I run the following, it always returns 0 even though the actual value is not 0:

MySqlCommand comm = connect.CreateCommand(); comm.CommandText = insertInvoice; comm.CommandText += '\'' + invoiceDate.ToString('yyyy:MM:dd hh:mm:ss') + '\', ' + bookFee + ', ' + adminFee + ', ' + totalFee + ', ' + customerID +  ')'; int id = Convert.ToInt32(comm.ExecuteScalar()); 

According to my understanding, this should return the ID column, but it just returns 0 every time. Any ideas?

EDIT:

When I run:

'INSERT INTO INVOICE (INVOICE_DATE, BOOK_FEE, ADMIN_FEE, TOTAL_FEE, CUSTOMER_ID) VALUES ('2009:01:01 10:21:12', 50, 7, 57, 2134);last_insert_id();' 

I get:

{'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'last_insert_id()' at line 1'} 
  • 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. 2026-05-11T01:18:18+00:00Added an answer on May 11, 2026 at 1:18 am

    [Edit: added ‘select’ before references to last_insert_id()]

    What about running ‘select last_insert_id();‘ after your insert?

    MySqlCommand comm = connect.CreateCommand(); comm.CommandText = insertInvoice; comm.CommandText += '\'' + invoiceDate.ToString('yyyy:MM:dd hh:mm:ss') + '\', '       + bookFee + ', ' + adminFee + ', ' + totalFee + ', ' + customerID +  ');';     + 'select last_insert_id();'  int id = Convert.ToInt32(comm.ExecuteScalar()); 

    Edit: As duffymo mentioned, you really would be well served using parameterized queries like this.


    Edit: Until you switch over to a parameterized version, you might find peace with string.Format:

    comm.CommandText = string.Format('{0} '{1}', {2}, {3}, {4}, {5}); select last_insert_id();',   insertInvoice, invoiceDate.ToString(...), bookFee, adminFee, totalFee, customerID); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can add a meta-tag like this. <meta http-equiv="pragma" content="no-cache"… May 11, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer I've used PYODBC to connect succesfully to an MS Access… May 11, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer One simple approach that might work for you is to… May 11, 2026 at 5:15 pm

Related Questions

I have a table in SQLite: CREATE TABLE EventType ( [EventTypeID] INTEGER PRIMARY KEY,
So say I have a products table with a list of products, and one
I'm trying to determine what situations MySQL updates an index. Say I have the
I have a live search on my help page that searches our help database

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.