how to get the primary key for a new row
-
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
-
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
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
-
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
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
-
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
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
-
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
-
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
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
-
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
-
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