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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:15:15+00:00 2026-05-16T23:15:15+00:00

I am trying to complete a seemingly simple task that has turned into a

  • 0

I am trying to complete a seemingly simple task that has turned into a several hour adventure: Getting @@Identity from TableAdapter.Insert().

Here’s my code:

protected void submitBtn_Click(object sender, EventArgs e)
{
    AssetsDataSetTableAdapters.SitesTableAdapter sta = new AssetsDataSetTableAdapters.SitesTableAdapter();
    int insertedID = sta.Insert(siteTxt.Text,descTxt.Text);

    AssetsDataSetTableAdapters.NotesTableAdapter nta = new AssetsDataSetTableAdapters.NotesTableAdapter();
    nta.Insert(notesTxt.Text, insertedID, null,null,null,null,null,null);
    Response.Redirect("~/Default.aspx");
}

One answer suggests all I may have to do is change the ExecuteMode. I tried that. This makes GetData() quit working (because I’m returning a scalar now instead of rowdata) (I need to keep GetData()). It also does not solve the issue in that the insertedID variable is still set to 1.

I tried creating a second TableAdapter in the TypedDataSet.XSD and setting the property for that adapter to “scalar”, but it still fails with the variable getting a value of 1.

The generated insert command is

INSERT INTO [dbo].[Sites] ([Name], [Description]) VALUES (@Name, @Description);
SELECT Id, Name, Description FROM Sites WHERE (Id = SCOPE_IDENTITY())

And the “Refresh the Data Table” (adds a select statement after Insert and Update statements to retrieve Identity” is also set.

Environment

SQL Server 2008 R2, Visual Studio 2010, .NET 4, Windows XP, all local same machine.

What’s causing this?

EDIT/UPDATE

I want to clarify that I am using auto-generated code within Visual Studio. I don’t know what the “tool” that generated the code is, but if you double click the *.XSD file it displays a UI of the SQL Table Schema’s and associated TableAdapter’s. I want to keep using the auto-generated code and somehow enable getting the Identity. I don’t want to write this all by hand with stored procedures.

  • 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-05-16T23:15:16+00:00Added an answer on May 16, 2026 at 11:15 pm

    Here’s my SQL Code that works.

    CREATE PROCEDURE [dbo].[Branch_Insert]
    (
        @UserId uniqueidentifier,
        @OrganisationId int,
        @InsertedID int OUTPUT
    )
    AS
        SET NOCOUNT OFF;
    INSERT INTO [Branch] ([UserId], [OrganisationId]) 
    VALUES (@UserId, @OrganisationId);
    
    SELECT Id, UserId, OrganisationId FROM Branch WHERE (Id = SCOPE_IDENTITY())
    SELECT @InsertedID = SCOPE_IDENTITY()
    

    Then when I create the Table Adapter – I can instantly see the @InsertedID parameter.

    Then from code, all I do is:

    int? insertedId = 0;
    branchTA.Insert(userId, orgId, ref insertedId);
    

    I’m not 100% whether using ref is the best option but this works for me.

    Good luck.

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

Sidebar

Related Questions

I'm trying to complete a regular expression that will pull out matches based on
I am beginner trying to complete a simple JSON problem on SingPath which asks
I am trying to have a textBox auto complete with values from a database.
I'm trying to complete an exercise to write a program that takes the following
I am trying to complete the tutorial at http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=438 It seems that the servlet
I trying to complete this exercise; Write a Lisp function that takes as input
I'm trying to complete a practice question from a book on generics but the
I'm having a hardcore head-ache from trying to get complete programmatic control over rendering
I am trying to implement auto complete via jquery auto complete plugin.A simple auto
im trying to getting auto complete working and i can do so fine when

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.