Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Retrieve the PK after an add into an in memory DataTable

Retrieve the PK after an add into an in memory DataTable

Scheduled Pinned Locked Moved C#
questioncsharpdatabaseperformancehelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I know how to do this in SQL but I must do it from C#. The DataTable is actually in memory (an xsd). So I have some DataSet called "Converted" and in it is a Table called "Results" which has a relationship with another table called "Descriptions". I am converting some data (thus the name) to an in memory table I have created. The problem is that I need the Primary Key of the "Results" last insert in order to populate the "Descriptions". How can I get the last inserted primary key? The bellow code shows what I am doing. In the section Descriptions is where various paramaters from the raw data are then combined into multiple entries into the Descriptions table. I ofcourse need to link them back to the current row I am processing.

            foreach (RawData.ResultsRow rRow in \_rawData.Results)
            {
                Converted.ResultsRow nRow = MemDataSet.Results.NewResultsRow();
                nRow.Param = rRow.SomeParam
                MemDataSet.Results.AddResultsRow(nRow);
    
                //int pk = ??????
                ...
                #region Descriptions
                ConvertedDescriptionsRow dRow = MemDataSet.Descriptions.NewDescriptionsRow();
                dRow.Param = rRow.SomeDescriptor
                dRow.ResultsFK = pk;
                MemDataSet.Descriptions.AddDescriptionsRow(dRow);
                #endregion Descriptions
            }
    

    "9 Pregnent woman can not have a baby in 1 month" -Uknown

    E Z 2 Replies Last reply
    0
    • L Lost User

      I know how to do this in SQL but I must do it from C#. The DataTable is actually in memory (an xsd). So I have some DataSet called "Converted" and in it is a Table called "Results" which has a relationship with another table called "Descriptions". I am converting some data (thus the name) to an in memory table I have created. The problem is that I need the Primary Key of the "Results" last insert in order to populate the "Descriptions". How can I get the last inserted primary key? The bellow code shows what I am doing. In the section Descriptions is where various paramaters from the raw data are then combined into multiple entries into the Descriptions table. I ofcourse need to link them back to the current row I am processing.

              foreach (RawData.ResultsRow rRow in \_rawData.Results)
              {
                  Converted.ResultsRow nRow = MemDataSet.Results.NewResultsRow();
                  nRow.Param = rRow.SomeParam
                  MemDataSet.Results.AddResultsRow(nRow);
      
                  //int pk = ??????
                  ...
                  #region Descriptions
                  ConvertedDescriptionsRow dRow = MemDataSet.Descriptions.NewDescriptionsRow();
                  dRow.Param = rRow.SomeDescriptor
                  dRow.ResultsFK = pk;
                  MemDataSet.Descriptions.AddDescriptionsRow(dRow);
                  #endregion Descriptions
              }
      

      "9 Pregnent woman can not have a baby in 1 month" -Uknown

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      I'm not going to give you the answer but only because you put regions inside of methods.

      Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

      M 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        I'm not going to give you the answer but only because you put regions inside of methods.

        Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        What sort of answer is that, he can have regions inside a method (NOT), how about nested regions and regions from the middle of one method to the middle of another method. I must admit I have never seen regions used inside a method

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • L Lost User

          I know how to do this in SQL but I must do it from C#. The DataTable is actually in memory (an xsd). So I have some DataSet called "Converted" and in it is a Table called "Results" which has a relationship with another table called "Descriptions". I am converting some data (thus the name) to an in memory table I have created. The problem is that I need the Primary Key of the "Results" last insert in order to populate the "Descriptions". How can I get the last inserted primary key? The bellow code shows what I am doing. In the section Descriptions is where various paramaters from the raw data are then combined into multiple entries into the Descriptions table. I ofcourse need to link them back to the current row I am processing.

                  foreach (RawData.ResultsRow rRow in \_rawData.Results)
                  {
                      Converted.ResultsRow nRow = MemDataSet.Results.NewResultsRow();
                      nRow.Param = rRow.SomeParam
                      MemDataSet.Results.AddResultsRow(nRow);
          
                      //int pk = ??????
                      ...
                      #region Descriptions
                      ConvertedDescriptionsRow dRow = MemDataSet.Descriptions.NewDescriptionsRow();
                      dRow.Param = rRow.SomeDescriptor
                      dRow.ResultsFK = pk;
                      MemDataSet.Descriptions.AddDescriptionsRow(dRow);
                      #endregion Descriptions
                  }
          

          "9 Pregnent woman can not have a baby in 1 month" -Uknown

          Z Offline
          Z Offline
          zlezj
          wrote on last edited by
          #4

          Not possible.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups