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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:43:41+00:00 2026-05-13T01:43:41+00:00

I came across a problem while updating a typed DataTable that has a primary

  • 0

I came across a problem while updating a typed DataTable that has a primary key column.
At some point in my code I fill DataTables (some of them have primary and foreign key columns) and then I insert the data of all DataTables in one transaction using DataAdapters and Update(). Because the typed DataTables do not allow the PrimaryKey table to be empty I insert some integer values in there. After calling Update() I expected the PK columns to be updated with the database PKs.

    public void UpdateMethod(DbTransaction transaction)
{
 DbDataAdapter dataAdapter = mDbProviderFactory.CreateDataAdapter();
 using (DbCommand insertCommand = CreateCommand())
 {
  insertCommand.Connection = mDbConnection;
  insertCommand.Transaction = transaction;
  dataAdapter.InsertCommand = insertCommand;
  dataAdapter.Update(dataTable);
 }

 // not sure if i need to do this:
 dataTable.AcceptChanges();

 // I would expect that databaseId is now the Id used in the database,
 // but it is the original Id which I set while creating the row entry
 databaseId = (int)dataTable.Rows[0]["Id"];
}

private DbCommand CreateCommand()
{
 // Make command object.
 DbCommand cmd = mDbProviderFactory.CreateCommand();

 // add command input parameters
 DbParameter parameter1 = mDbProviderFactory.CreateParameter();
 parameter1.ParameterName = mDatabaseParameterPrefix + "someColumn";
 parameter1.SourceColumn = "someColumn";
 parameter1.Size = 255;
 parameter1.DbType = DbType.String;

 // Output parameter
 DbParameter idParameter = mDbProviderFactory.CreateParameter();
 idParameter.ParameterName = mDatabaseParameterPrefix + "ID";
 idParameter.SourceColumn = "ID";
 idParameter.Direction = ParameterDirection.InputOutput;
 idParameter.DbType = DbType.Int32;

 // setup sql command
 cmd.Parameters.Add(parameter1);
 cmd.Parameters.Add(idParameter)
 cmd.CommandText = @"INSERT INTO [SomeTable] ([someColumn], ...) VALUES(@someColumn, ... ) SELECT CAST(SCOPE_IDENTITY() AS int) AS 'ID'";
 cmd.UpdatedRowSource = UpdateRowSource.Both;

 return cmd;
}

Thanks for any hints!

  • 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-13T01:43:41+00:00Added an answer on May 13, 2026 at 1:43 am

    Solved it. The problem was the SQL statement: Instead of

    INSERT INTO [SomeTable] ([someColumn], …) VALUES(@someColumn, … ) SELECT CAST(SCOPE_IDENTITY() AS int) AS ‘ID’

    It must be:

    INSERT INTO [SomeTable] ([someColumn], …) VALUES(@someColumn, … ) SELECT @ID = SCOPE_IDENTITY()

    Otherwise the value of the primary key is not assigned to the output parameter.

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

Sidebar

Related Questions

I came across this problem while preparing for an interview and curious to know
While investigating a reported problem with my site loading slowly, I came across an
I came across a problem in my current application that required fiddling with the
While developing part of a simulator I came across the following problem. Consider a
The Problem It's something I came across a while back and was able to
Today I came across a problem where someone had accidentally committed a proj.user file
I am new to creating Java web applications and came across this problem when
After getting fine answer to my previous question , I came across another problem.
So I came across an interesting problem today. We have a WCF web service
I'm still trying to debug a very sneaky memory corruption problem. I came across

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.