Get the last inserted row's Identity (ID) using DataTable and Row


I use DataTable and Row and the Update() method to insert data into my table.

I need to retrieve the Identity (ID) assigned to that row for further processing. (e.g. add data into other tables that refer to this row).

How do I retrieve the ID? 


In a high traffic website (e.g. 100 inserts in 5 seconds), by just retrieving the ID of the last inserted row may not be very safe. Please advise if I am correct.

Any other methods of achieving the same goal?

thanks in advance

4/18/2008 5:54:28 AM 16182 articles. 0 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 9

string query = "Insert Into yourTableName("col1","col2") values(xyz,pqr) select @@Identity as YourFieldName "

 now open a conncetion and fire a query

long _id = Convert.ToInt64(command.ExecuteScalar());

later you can use _id for further processing.


Watch The true story of internet ||Yet another forum |||
I'm big fan of Open source flash chart for .net very very simple
I n t e r v i e w T i p s
4/18/2008 6:27:04 AM

hi, thanks for the reply. I'm using the DataTable, so I'm not sure how to incorporate your proposed solution.


Currently I have the following code:

        Product.ItemDataTable itemTable = newProduct.ItemDataTable();
        Item.ItemRow item = itemTable.NewItemRow();

        // Add some data here.......

        // Add the new product
        int rowsAffected = itemAdapter.Update(itemTable);



4/19/2008 3:15:11 AM

Are you updating or inserting by that way?
 Which query or storeprocedure you have writte for it?

Watch The true story of internet ||Yet another forum |||
I'm big fan of Open source flash chart for .net very very simple
I n t e r v i e w T i p s
4/19/2008 6:51:07 AM


I'm inserting new record.

I've written only the SQL insert for it. 

4/19/2008 1:04:25 PM

Similar Artilces:

Database: Inserting rows and getting their ID's using the Update(DataTable) function
Hello Everybody,I am using the following approach of inserting date to the data base:1 // Create a new row.2 NorthwindDataSet.RegionRow newRegionRow;3 newRegionRow = northwindDataSet.Region.NewRegionRow();4 // newRegionRow.RegionID = 5; <- this Id is generated automatically5 newRegionRow.RegionDescription = "NorthWestern";6 7 // Add the row to the Region table8 this.northwindDataSet.Region.Rows.Add(newRegionRow);9 10 // Save the new row to the database11 this.regionTableAdapter.Update(this.northwindDataSet.Region);from:

Is having a trigger that inserts a row in Table 'A', when a row in same table is inserted by ADo.Net code?
I want to insert a row for a Global user  in Table 'A' whenever ADO.Net code inserts a Local user row into same table. I recommended using a trigger to implement this functionality, but the DBA was against it, saying that stored proecedures should be used, since triggers are unreliable and slow down the system by placing unecessary locks on the table. Is this true OR the DBA is saying something wrong? My thinking is that Microsoft will never include triggers if they are unreliable and the DBA is just wanting to offload the extra DBA task of triggers to the programmer so that a s...

how I can get last inserted row in triggre After Insert Row.
Hello all , I written one trigger After row Insert now i want to update some column after row insert but please tell me how i can get last inserted row (last immediately inserted row) so that i can immidiatly Update the row. my trigger is here : ALTER TRIGGER Uldefaultvalue AFTER INSERT ORDER 1 ON "DBA"."style" REFERENCING NEW AS u FOR EACH ROW /* WHEN ( search-condition ) */ BEGIN Declare an_id numeric(9,2) ; select s_id into an_id from style ; update style set customer = 'cust' where s_id = an_id ; END here it is giving err...

How to get immediate inserted row and it's ID column value ?
Hello All , Gole is that i want to get value of Identity Column of immediatly inserted row. I am have a datastore and I am inserting a row in it and after that immediatly i want value of identity column, like below here i am doing li_row = lds_qb.insertrow(0) // Setting values to datastore of QuickBooks. lds_qb.setItem(li_row , 'name',dwc_master.getItemString(li_row_m,"name")) lds_qb.setItem(li_row , 'vendoraddressaddr1',dwc_master.getItemString(li_row_m,"address1")) lds_qb.setItem(li_row , 'vendora...

Inserting all rows of a datatable into Oracle Table in a single shot, not row by row using C#
Hi All... I have imported the data of CSV File into Datable using C#. Now i need to insert the content of DataTable to the table in Oracle Database in a single shot, without looping through the rows of the datatable. Is this Possible? Please help me out by providing the sample code. Below is sample code for Importing the CSV Data to Dataset string connString =string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;",Path.GetDirectoryName(filename)); string cmdString =string.Format("SELECT * FROM {0}", Path.G...

How to stop the last detail row to print on next page together with the group trailer and summary even the second last page has lot's of space for the last row but not for the summary...
Hi, Can anyone please help me on this : My reports have quite a lot in the group trailers and summary, so that the last detail row will print on the last page with the trailers and summary even though the second last page has a lot of spaces for the detail, but just not enough for the trailers and summary. Therefore it looks very odd that there are lots of space in the second last page, and clients thought the report is finished while the last detail row is printed on the every end page. How can I force the report to fill up the second last page before printing the las...

Need to get row id after insert. Can't get my syntax working.
 Hi,I'm trying to get the identity of the row after an insert, but I can't get the syntax right.  Could someone with better eyeballs help me out? Here is my Insert stuff..  <asp:SqlDataSource ID="SqlDataSourceQueue" runat="server" ConnectionString="<%$ ConnectionStrings:ServiceQConnectionString %>" SelectCommand="SELECT * FROM [queue] WHERE ([caseno] = @caseno)" DeleteCommand="DELETE FROM [queue] WHERE [id] = @id" InsertCommand="INSERT INTO queue (caseno...

How do I get the row id of the last insert?
If I insert a row into mySQL with an indexed auto incrementing column, id, how do I get the id of the last inserted row? Thanks in advance, Dan Dan Anderson said: > If I insert a row into mySQL with an indexed auto incrementing column, > id, how do I get the id of the last inserted row? Use the LAST_INSERT_ID() function. Or, see these pages: By the way, this isn't the place to ask that question. Colin -- Colin W. Wetherbee http://hydrogen.denterprises.o...

Getting id of last row inserted.
I am using MS Access as database and want to retrieve the id of the last row inserted by current user. Please note, the system can be accessed by multiple user at same time.   Any help ??  See I am learning :)If this post was useful to you, please mark it as answer. Thank you! Thanks for the help. But, I am using TableAdapters. Any help. Regards Mike [MVP - ASP/ASP.NET]My site...

Getting the last inserted row ID
Hey all, Just started to use ASP.NET and jumped in at the deep end using the new 2.0 version with VS2005. I've got a data source from which i call an insert command, this works fine. however, i would like to retrieve the ID of the row i have just inserted and store it in a variable in my vb code. How would i go about doing it? So i'm using ASP.NET with VB.NET 2.0 and VS2005 Beta Refresh. I hope you can help me as its really doing my head in now Cheers tom Take a look at this post on my blog/Fredrik Normén - fredrikn @ twitterMicrosoft MVP, MCSD, MCAD, MCTASPInsidersMy B...

Row ID of the newly inserted row FormView using AccessdataSource
How can I get the AccessDatasource Row ID of the newly inserted row when adding anew record to the database using a FormView Control on a web page?I've looked at the FormView_ItemInserted FormViewInsertedEventArgs but to noavail. can some one give me sample code for it thanks   Hi:   Please check out this article:   The idea is to use ViewState to store the id of newly inserted record. RegardsSincerely,Allen ChenMicrosoft Online Com...

DLink -get last row inserted identity
hello everyone, I am using DLink (Link for Database) in visual studio 2008.does anyone knows how to get the identity of an inserted row?in sql server in stored procedure we used to write @@identitybut how do we do it in DLink.??? if we don't want to call a stored procedure The Things That doesnot kill you make, you stronger...

Can't get dw_1.Object.Data[row] for a row > 32768
Is anyone aware of a problem with passing a row greater than MAX(integer) (32768) to dw_1.Object.Data[row]? I would appreciate a response. Jameel Abdo MCI Systemhouse. I understand that PB5 didn't support object notation for more than 32K rows. It was supposed to be fixed in PB6 (although I haven't confirmed it in PB6, myself). The object notation tended to be a tad flakey in PB5 too (poor null handling, etc.). Many developers decided to stick with the GetItem, SetItem in PB5 to avoid these problems. Jameel Stephen Abdo wrote: > Is anyone aware of a proble...

Looping through rows of a DataGridView who's rows may be getting deleted within the loop?
Hi everyone, I have a DataGridView that is populated with data. In one of my procedures, I'm looping through each row of the DGV, and with each pass in the loop, there's a possibility that the row that is being worked on may be deleted.  The problem is that I've started the for loop using 0 to intRowCount (which represents the number of rows.  If I don't delete any rows, everything works fine.  If I delete a row, I'm guessing that the intRowCount is no longer accurate and in some situations points to a row that no longer exists, and I get an index out of ...

Web resources about - Get the last inserted row's Identity (ID) using DataTable and Row -

Resources last updated: 12/16/2015 12:42:30 AM