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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:48:10+00:00 2026-06-03T06:48:10+00:00

I have a Stored Procedure that look like this BEGIN insert into `TheSomaProject`.`Projects` (

  • 0

I have a Stored Procedure that look like this

BEGIN
insert into `TheSomaProject`.`Projects` 
( `ProjectNr`, `MachineName`, `MachineNameEng`, `Type_Id`, 
  `SerialNr`, `Description`, `Created`, `CompanyId`, `MachineType` ) 
values 
( param_ProjectNr, param_MachineName, param_MachineNameEng, param_TypeId, 
  param_Serial, param_Description, NOW(), param_CompId, param_MachineType); 
SELECT last_insert_id(); 
END

But the return dosent give me the Id, it’s looks like this 0x00000000. The C# code look like this

MySqlDataReader Reader;
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = "CALL stp_InsertProject(@param_ProjectNr, @param_MachineName, @param_MachineNameEng, @param_TypeId, @param_Serial, @param_Description, @param_CompId, @param_MachineType);";
cmd.Parameters.AddWithValue("@param_ProjectNr", projectNr);
cmd.Parameters.AddWithValue("@param_MachineName", MachineName);
cmd.Parameters.AddWithValue("@param_MachineNameEng", MachineNameEng);
cmd.Parameters.AddWithValue("@param_TypeId", TypeId);
cmd.Parameters.AddWithValue("@param_Serial", SerialNr);
cmd.Parameters.AddWithValue("@param_Description", Description);
cmd.Parameters.AddWithValue("@param_CompId", CompanyId);
cmd.Parameters.AddWithValue("@param_MachineType", MachineType);
conn.Open();
Reader = cmd.ExecuteReader();
while (Reader.Read())
{
    Id = Convert.ToInt32(Reader["last_insert_id()"]);
}
conn.Close();

But if i try to use the same stp_InsertProject in phpMyAdmin i get the correct id back.

  • 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-03T06:48:12+00:00Added an answer on June 3, 2026 at 6:48 am

    Your stored procedure does not return a result set with a column named ‘last_insert_id()’, thus using an MySqlDataReader will not work (as you expect).

    Use ExecuteScalar() to get the result. Note that the value returned is actually a 64-bit integer, so should use a Int64 (or long) type to hold the result:

    Int64 lastInsertId = (Int64)cmd.ExecuteReader();
    

    If you need to have a Int32 variable, and thus risk the overflow if too many rows are inserted, make sure you don’t directly cast to Int32 (or int) as this will cause an InvalidCastException since the return type isn’t really Int32 but Int64. In this case use Convert.ToInt32. But I would really suggest that you use a 64 bit integer.

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

Sidebar

Related Questions

I have a Pl/Sql procedure signature that look like this foo(param1 IN type1, param2
I have a stored procedure that logs some data, how can I call this
I have a stored procedure that uses this select statement: SELECT dbo.VendorProgram.id, dbo.VendorProgram.CODE, dbo.Programs.ProgramName
I have a SQL Server 2008 database. This database has a stored procedure that
This is stored procedure that worked on earlier version of mysql. Now, I have
I have a stored procedure that takes a user ID and calculates their balance
I have a stored procedure that takes no parameters, and it returns two fields.
I have a stored procedure that I want to call from within another, and
i have a stored procedure that performs a join of TableB to TableA :
I have a stored procedure that does, among other stuff, some inserts in different

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.