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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:42:42+00:00 2026-06-17T09:42:42+00:00

Browsed some answers and doesn’t appear to be working for me. I need the

  • 0

Browsed some answers and doesn’t appear to be working for me.

I need the ID field of a table to be returned so I can use it in a different part of the program, I’ve tried using

    Convert.ToInt32(sqlComm.ExecuteScalar());

But no luck, and same for

    Convert.ToInt32(sqlComm.Parameters["ID"].Value);

And both return 0, even though the record does get inserted into the table.

I’ll dump the code below, can anyone see what I’m doing wrong?

    using (SqlConnection sqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand sqlComm = new SqlCommand("up_Insert_Address", sqlConnect))
            {
                sqlComm.CommandType = CommandType.StoredProcedure;
                sqlComm.Parameters.Add("@AddressID", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                sqlComm.Parameters.Add("@AddressLineOne", SqlDbType.NVarChar, 40).Value = address.AddressLineOne;   

                try
                {
                    sqlComm.Connection.Open();
                    return Convert.ToInt32(sqlComm.ExecuteScalar());
                }

                catch (SqlException)
                {
                }

                finally
                {
                    sqlComm.Connection.Close();
                }
            }
        }

And Stored Procedure:

    @AddressID   Bigint OUTPUT,
    @AddressLineOne  NVarChar(40)
    AS
     BEGIN
      BEGIN TRY
      INSERT INTO Address
      (
         AddressLineOne
      )
      VALUES 
      (
         @AddressLineOne
      )

      SET @AddressID = SCOPE_IDENTITY();
    END TRY
    BEGIN CATCH
       DECLARE @Err nvarchar(500)
       SET @Err = ERROR_MESSAGE()
       RAISERROR(@Err, 16, 1)
    END CATCH
   END
  • 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-17T09:42:42+00:00Added an answer on June 17, 2026 at 9:42 am

    You should be using

    Convert.ToInt64(sqlComm.Parameters["@AddressID"].Value);
    

    after you execute the command using ExceuteNonQuery. For future reference, ExecuteScalar returns the first column of the first row in the result set returned by the query. You’re not returning anything, just setting the value of an OUTPUT parameter.

    Also you should DEFINITELY not swallow any SqlException. Since your command and connection are already in using blocks you don’t need to add another try/catch/finally. Change it to to:

    //try
    //{
        sqlComm.Connection.Open();
        sqlComm.ExecuteNonQuery();
        return Convert.ToInt64(sqlComm.Parameters["@AddressID"].Value);
        // using Int64 since the SQL type is BigInt
    //}
    
    //catch (SqlException)
    //{
    //}
    
    //finally
    //{
    //    sqlComm.Connection.Close();
    //}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've been working on this for some time now but I can't seem to
I've made a website, to test the html5 audio. Unfortunately some browser doesn't support
How can I easily open a new browser with just some help text in
When I'm on GitHub and I browse some folder, the content loads dynamically :
I'm writing some browser side dynamic functionality and using HTTP Basic Auth to protect
I'm already aware of some browser-specific DOM documentation out there (Gecko DOM Reference for
I'm trying to do some browser automation, but I'm having some problems. Basically, what
Question text/background and finds My browser sends some HTTP header (e.g. referer) to my
I've got some strange bug: when I open page first time in some browser
I'd like to produce following behaviour: User in the browser saves some entity. After

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.