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 67k
  • Answers 67k
  • 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
  • added an answer They're common. If you can get them to remove it,… May 11, 2026 at 11:48 am
  • added an answer You could try the code that is built as part… May 11, 2026 at 11:48 am
  • added an answer This is probably because IE6/7 don't have gears installed. That… May 11, 2026 at 11:48 am

Related Questions

I have a query to insert a row into a table, which has a
I have a query to the effect of SELECT t3.id, a,bunch,of,other,stuff FROM t1, t2,
If I have a query to return all matching entries in a DB that
I'm writing a query to summarize some data. I have a flag in the
I'm doing a probability calculation. I have a query to calculate the total number
I have a query where I wish to retrieve the oldest X records. At
I have a query which is meant to show me any rows in table
I have a query where i want to get a distinct description by the
I have a query that is currently using a correlated subquery to return the
I have a query that I'm executing from a .NET application to a SQL

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.