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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:07:07+00:00 2026-06-13T21:07:07+00:00

Having read around this site I thought the following code would return the ID

  • 0

Having read around this site I thought the following code would return the ID of the item I add to my database but the int value I am trying to fill isn’t showing up in scope. Here is the code for my insert statement:

  foreach (ExOb exc in exobject)
            {

                Type type = exc.GetType();
                //Add a weight exercise
                if (type == typeof(Weights))
                {

                    Weights thisweight = (Weights)exc;
                    string InsertWeight = ("INSERT INTO WeightExercise (ExcerciseName, Duration, Sets, DiaryId) VALUES (@name, @time, @sets, @DiaryId) SET @ID = SCOPE_IDENTITY();");

                        com = new SqlCommand(InsertWeight, con);
                        com.Parameters.AddWithValue("@name", thisweight.ExcerciseName);
                        com.Parameters.AddWithValue("@time", thisweight.WeightDuration);
                        com.Parameters.AddWithValue("@sets", thisweight.TotalSets);
                        com.Parameters.AddWithValue("@DiaryId", results[0]);
                        com.Parameters.Add("@ID", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        con.Open();
                        com.ExecuteNonQuery();
                        int ID = (int)com.Parameters["@ID"].Value;

                        con.Close();


                }
                else if (type == typeof(Cardio))
                {
                    Cardio thiscardio = (Cardio)exc;


                }

            }
        }

The database is updated with the Weight Exercise details. I have checked when debugging and the command parameter @ID holds the ID of the latest entry. Int ID does not show up in locals when debugging and if I Watch it I get:

    ID  The name 'ID' does not exist in the current context 

What am I doing wrong here?

  • 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-13T21:07:09+00:00Added an answer on June 13, 2026 at 9:07 pm

    I have checked when debugging and the command parameter @ID holds the
    ID of the latest entry.

    This indicates the database code you are currently using is functioning fine.

    Int ID does not show up in locals when debugging and if I Watch it I
    get…

    This is the root of your question. The problem is that you’ve scoped ID to inside the

    if (type == typeof(Weights))
    

    block. Outside of that block, the identifier ID has no meaning.

    Declare ID at the top of the scope for this procedure and you should be able to see it in Locals or add a Watch.

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

Sidebar

Related Questions

Having read around it appears this should be possible. I have the following content
Having read other people's questions I thought window.onload=... would answer my question. I have
Okay, so I've read around on the Oracal site and some questions on this
Having read this link on RBAR and this , my understanding of RBAR amounts
Having read this question Immutable or not immutable? and reading answers to my previous
After googling and landing on SO and having read this other question Is it
I'v read around that you could bind different events using bind() function obviously. But
I've read around and found this answered question about a problem relating to this
I am having a little difficulty trying to wrap my head around site structures.
Having read this article and many others out there on how to not store

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.