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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:14:08+00:00 2026-06-12T21:14:08+00:00

Maybe someone else looking at this code will be able to tell me why

  • 0

Maybe someone else looking at this code will be able to tell me why the returnID is always 0. I am attempting to retrieve the new ID from the inserted record.

public int AddToInventory(int PartID, int QtyOnHand, int SpokenFor, int LowOrderQty, int HighOrderQty, decimal LastBuyPrice, 
                                    decimal AvgBuyPrice)

        {
        ConfigDAL config = new ConfigDAL();
        string connstr = config.GetConnString();
        SqlConnection conn = new SqlConnection(connstr);

        string query;
        query = "INSERT INTO Inventory (PartID, QtyOnHand, SpokenFor, LowOrderQty, HighOrderQty, LastBuyPrice, "
                    + "AvgBuyPrice, CreatedOn, CreatedBy, ModifiedOn, ModifiedBy) "
                    + "Values (@PartID, @QtyOnHand, @SpokenFor, @LowOrderQty, @HighOrderQty, @LastBuyPrice, @AvgBuyPrice, "
                    + "@CreatedOn, @CreatedBy, @ModifiedOn, @ModifiedBy)";

        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@PartID", PartID);
        cmd.Parameters.AddWithValue("@QtyOnHand", QtyOnHand);
        cmd.Parameters.AddWithValue("@SpokenFor", SpokenFor);
        cmd.Parameters.AddWithValue("@LowOrderQty", LowOrderQty);
        cmd.Parameters.AddWithValue("@HighOrderQty", HighOrderQty);
        cmd.Parameters.AddWithValue("@LastBuyPrice", LastBuyPrice);
        cmd.Parameters.AddWithValue("@AvgBuyPrice", AvgBuyPrice);
        cmd.Parameters.AddWithValue("@CreatedOn", DateTime.Now);
        cmd.Parameters.AddWithValue("@CreatedBy", GlobalProp.UserName);
        cmd.Parameters.AddWithValue("@ModifiedOn", DateTime.Now);
        cmd.Parameters.AddWithValue("@ModifiedBy", GlobalProp.UserName);
        cmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        int returnID = (int)cmd.Parameters["@ID"].Value;

        return returnID;
        }

The record gets inserted to the table fine, but the return value is not right. Am I doing this correctly?

Thanks

  • 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-12T21:14:10+00:00Added an answer on June 12, 2026 at 9:14 pm

    There is no where you were setting the ID, so you can expect the value to change. You have to do that with

     Select @ID = @Scope_Identity() -- If ID column is an Identity column
    

    OR

    Select @ID = @SomeGeneratedValue
    

    Try this

    query = "INSERT INTO Inventory (PartID, QtyOnHand, SpokenFor, LowOrderQty, HighOrderQty, LastBuyPrice, "
                    + "AvgBuyPrice, CreatedOn, CreatedBy, ModifiedOn, ModifiedBy) "
                    + "Values (@PartID, @QtyOnHand, @SpokenFor, @LowOrderQty, @HighOrderQty, @LastBuyPrice, @AvgBuyPrice, "
                    + "@CreatedOn, @CreatedBy, @ModifiedOn, @ModifiedBy);"
                    + " Declare @ID int;"
                    + " Select @ID = Scope_Identity()";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes one needs to dig into someone else's code, understand it and maybe refactor/fix
This question is mainly directed at the PrimeFaces dev team but maybe someone else
I am probably overlooking something obvious but then maybe someone else will too. I'm
I am struggling really hard to understand this behavior so maybe someone can shed
I find this question a little tricky. Maybe someone knows an approach to answer
Hi if someone could look at my code and maybe see something I am
I'm looking to export the Java SE 7 Documentation from HTML into parse-able XML.
I'm ask/answering this question because it hung me up & it's likely someone else
I thought I'd post here in the hope that maybe someone with MVVM expertise
I'm having trouble groking something in Linq - maybe someone can give me some

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.