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. Managed C++/CLI
  4. how to get the primary key for a new row

how to get the primary key for a new row

Scheduled Pinned Locked Moved Managed C++/CLI
databasehelptutorialquestionannouncement
8 Posts 3 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.
  • S Offline
    S Offline
    Stefan Baens
    wrote on last edited by
    #1

    I have problem retrieving the primary key for a new row that is added to the database. This is what I have now:

    	// 9. Nieuwe rij maken in tabel
    	DataTable^ MyTable = myDataSet->Tables\[0\];
    	DataRow^ row = MyTable->Rows->Add();
    	if (row != nullptr)
    
    	{
    		// 10. Datavelden invullen
    		row\["item\_id"\] = item\_id;
    		row\["use"\] = use;
    		row\["type"\] = Convert::ToInt16(type);
    
    		// 11. Data updaten + Primary Key opvragen
    		DataAdapter->Update(myDataSet, "tblPanel");
    		PanelID = Convert::ToInt64(row\["ID"\]);
    	}
    

    now the value PanelID is always 0 for the first time this loop runs, the second time and further the information is correct and I can use the PanelID to create the relation with other tables. What am I missing?

    Stefan

    G L 2 Replies Last reply
    0
    • S Stefan Baens

      I have problem retrieving the primary key for a new row that is added to the database. This is what I have now:

      	// 9. Nieuwe rij maken in tabel
      	DataTable^ MyTable = myDataSet->Tables\[0\];
      	DataRow^ row = MyTable->Rows->Add();
      	if (row != nullptr)
      
      	{
      		// 10. Datavelden invullen
      		row\["item\_id"\] = item\_id;
      		row\["use"\] = use;
      		row\["type"\] = Convert::ToInt16(type);
      
      		// 11. Data updaten + Primary Key opvragen
      		DataAdapter->Update(myDataSet, "tblPanel");
      		PanelID = Convert::ToInt64(row\["ID"\]);
      	}
      

      now the value PanelID is always 0 for the first time this loop runs, the second time and further the information is correct and I can use the PanelID to create the relation with other tables. What am I missing?

      Stefan

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      From my experience, the primary is usually created at the database server via a trigger or stored procedure. You can send your data to be added to a store procedure which can return newly created primary key.

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      S 1 Reply Last reply
      0
      • G George L Jackson

        From my experience, the primary is usually created at the database server via a trigger or stored procedure. You can send your data to be added to a store procedure which can return newly created primary key.

        "We make a living by what we get, we make a life by what we give." --Winston Churchill

        S Offline
        S Offline
        Stefan Baens
        wrote on last edited by
        #3

        I have been looking at different site and manuals for an example but have not found one containing a stored procedure. Mostly I found some SQL way to find it but it has the same problem. Strange thing is that only the first time is not working and I can't find any missing declarations.

        Stefan

        G 2 Replies Last reply
        0
        • S Stefan Baens

          I have been looking at different site and manuals for an example but have not found one containing a stored procedure. Mostly I found some SQL way to find it but it has the same problem. Strange thing is that only the first time is not working and I can't find any missing declarations.

          Stefan

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          I am at work right now and cannot provide an example of how I solved the problem. I have used both Oracle and Sql Server store procedures to obtain the primary key. However, I believe there are some tricky spots.

          "We make a living by what we get, we make a life by what we give." --Winston Churchill

          1 Reply Last reply
          0
          • S Stefan Baens

            I have problem retrieving the primary key for a new row that is added to the database. This is what I have now:

            	// 9. Nieuwe rij maken in tabel
            	DataTable^ MyTable = myDataSet->Tables\[0\];
            	DataRow^ row = MyTable->Rows->Add();
            	if (row != nullptr)
            
            	{
            		// 10. Datavelden invullen
            		row\["item\_id"\] = item\_id;
            		row\["use"\] = use;
            		row\["type"\] = Convert::ToInt16(type);
            
            		// 11. Data updaten + Primary Key opvragen
            		DataAdapter->Update(myDataSet, "tblPanel");
            		PanelID = Convert::ToInt64(row\["ID"\]);
            	}
            

            now the value PanelID is always 0 for the first time this loop runs, the second time and further the information is correct and I can use the PanelID to create the relation with other tables. What am I missing?

            Stefan

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            http://msdn2.microsoft.com/en-us/library/ks9f57t0(VS.71).aspx[^]

            S 1 Reply Last reply
            0
            • S Stefan Baens

              I have been looking at different site and manuals for an example but have not found one containing a stored procedure. Mostly I found some SQL way to find it but it has the same problem. Strange thing is that only the first time is not working and I can't find any missing declarations.

              Stefan

              G Offline
              G Offline
              George L Jackson
              wrote on last edited by
              #6

              led mike's post refers to what I am talking about. I also have a different solution that uses a stored procedure but it was designed to do some extra fancy stuff that may be overkill for what you have in mind. Geo

              "We make a living by what we get, we make a life by what we give." --Winston Churchill

              1 Reply Last reply
              0
              • L led mike

                http://msdn2.microsoft.com/en-us/library/ks9f57t0(VS.71).aspx[^]

                S Offline
                S Offline
                Stefan Baens
                wrote on last edited by
                #7

                I checked out the site and found the things I need I think. Just pity that microsoft provides only examples in VB and C#.

                Stefan

                L 1 Reply Last reply
                0
                • S Stefan Baens

                  I checked out the site and found the things I need I think. Just pity that microsoft provides only examples in VB and C#.

                  Stefan

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  Stefan Baens wrote:

                  I checked out the site and found the things I need

                  I'm glad you have a solution

                  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