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 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

Related Questions

I have a SQL Insert query inside a stored proc, for inserting rows into
I have a table which has essentially boolean values in a legacy database. The
I want to insert a row of data into a table with five columns
I am using the Informix.NET driver (C#) to insert a row into a database
I have a query that is dynamically built after looking up a field list
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
i have a input tag which is non editable, but some times i need
I've noticed that a few Wordpress blogs have query statistics present in their footer
I have a query where I wish to retrieve the oldest X records. At
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,

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.