OnTheAnvil Posted November 22, 2004 Posted November 22, 2004 I have a SQL Server 2000 database with a table "Customers" with a column "csID" with Identity Seed = 1 and Identity Increment = 1. Nothing fancy. When I save a row using the following code I would like to have the value of the csID returned back to me so that I can use it else where. I could write a query to go back to the database and find the row and then get the csID back out of that row but that seems like a waste. Is there anyway that I can refresh the drNewRow so that it will contain the csID that was automatically given to it when it was saved? '------------------------------------- Dim da As OleDb.OleDbDataAdapter da = returnPageDataAdapter()'Return a valid dataadapter for my table Dim dt As New DataTable() Dim drNewRow As DataRow da.Fill(dt) drNewRow = dt.NewRow() 'Code to populate the drNewRow dt.Rows.Add(drNewRow) da.Update(dt) 'Commit the changes to the database Thanks, OnTheAnvil Quote
*Gurus* Derek Stone Posted November 22, 2004 *Gurus* Posted November 22, 2004 Execute the following statement after the row in question is inserted/updated: SELECT @@IDENTITY; Quote Posting Guidelines
OnTheAnvil Posted November 22, 2004 Author Posted November 22, 2004 Execute the following statement after the row in question is inserted/updated: SELECT @@IDENTITY; Does that return that last identity created? Because if it does how can I be sure no other rows where created since I saved the row I'm working with. ~OnTheAnvil Quote
*Gurus* Derek Stone Posted November 23, 2004 *Gurus* Posted November 23, 2004 The SELECT @@IDENTITY statement has to be executed after the row is inserted/updated, and prior to any other rows being inserted/updated in order for it to return the "expected" value. Ideally, you should place your SQL code in a stored procedure and use the SCOPE_IDENTITY function to restrict the returned value to the current scope and session. Quote Posting Guidelines
Joe Mamma Posted November 23, 2004 Posted November 23, 2004 Get the ado.net cook book! Go to oreilly.com get the code for chapter 4.1 (i think thats it) No need to 'retrieve' the identity. set your identity increment to a negative number, on update the idenitiy will value will be the one generated on the server Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
OnTheAnvil Posted November 23, 2004 Author Posted November 23, 2004 Joe Mamma, You idea sounds like exactly what I want. Unfortunately I'm not close to a store that would sell any kind of computer books. Would you mind sending me a code snippet so I can see what your talking about. I'll try and order the book online but I'd like to get started on solving this problem. Thanks, OnTheAnvil Quote
Joe Mamma Posted November 27, 2004 Posted November 27, 2004 sorry, didn't see your reply. http://examples.oreilly.com/adonetckbk/ in the zip, .\Main\Chapter 04\AutoIncrementWithoutConflictForm.cs (or vb if you get that zip) Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.