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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:41:34+00:00 2026-06-07T05:41:34+00:00

I implemented code in asp.net for storing record and retrieving identity field value. here

  • 0

I implemented code in asp.net for storing record and retrieving identity field value. here is my code.

str.Append("Insert Into Table1(value1,value2)values(?value1,?value2);Select LAST_INSERT_ID();");
MySqlCommand cmd = new MySqlCommand(str.ToString(), con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new MySqlParameter("?value1", value1));
cmd.Parameters.Add(new MySqlParameter("?value2", value2));
if (con.State != ConnectionState.Open) 
  con.Open();
cmd.ExecuteNonQuery();
long id = Convert.ToInt64(cmd.ExecuteScalar());

When i check my database, each record posted twice. when i remove line.

 long id = Convert.ToInt64(cmd.ExecuteScalar());

Every thing fine, but not identity retrieved.
How can i implement code in order to retrieve identity field value and record posted one time only.

  • 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-07T05:41:35+00:00Added an answer on June 7, 2026 at 5:41 am

    ExecuteScalar executes the query and returns the result. ExecuteNonQuery executes the query. That means you are executing the query twice here by calling both. Get rid of the ExecuteNonQuery method and you will be good

    if (con.State != ConnectionState.Open) 
       con.Open();    
    
    long id = Convert.ToInt64(cmd.ExecuteScalar());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented some code using backbone.js in Asp.NEt MVC3 and found backbone.js very helpful.
I've implemented url routing under asp.net 3.5 with the following code: public IHttpHandler GetHttpHandler(RequestContext
i have implemented code like here but it's not working for iOS 5.0, it
I implemented a custom Profile object in code as described by Joel here: How
I implemented 6 months ago in our web application site(Asp.Net) a page where you
I have a service implemented in MVC4 / ASP.NET Web Api. I would like
I implemented membership in asp.net, I have a manager that can define users, users
I'm currently using an SqlDataSource in ASP.NET/C# to let users insert, delete and update
I implemented the membership in asp.net 4.0, I have 3 roles(manager,operator,user) and 2 user
I have this code implemented and I like how straight forward it is because

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.