Solved

Get index of newly inserted data adapter data set

Posted on 2004-03-25
4
377 Views
Last Modified: 2012-06-21
Hi,

I've added a new record into my sql table by adding a new row into a data set then updating using a data adapter.

However when i refill my dataset the newly inserted record is not in it :(

The new record has been made by the way, I need the index of the newly inserted record to maintain referential integrity
as I am updating another table straight after which needs the index of the newly inserted record.

I know (as I have before) that you can just grab the max index from the table and hope no new records have been
added between the insert and select max index queries.

Is their a better way of doing this?

Hope you can help as this is my first .net project,
Dan
0
Comment
Question by:danvonnewman
4 Comments
 
LVL 6

Accepted Solution

by:
pillbug22 earned 25 total points
ID: 10676955
using

@@IDENTITY

in your SQL statement will grab the row ID of the last inserted row:

INSERT INTO tblTable
(name, number)
VALUES
('you', 2)
SELECT @@IDENTITY
0
 

Assisted Solution

by:Jarrin
Jarrin earned 25 total points
ID: 10676978
Yes that is common problem.

My solution in past was this:

I create new table named example ControlHead
there are so many records as are indexes and i increase them everytime when i insert new record.

So i get new ID and in same time increase it. If i don't use it the next insert will also increase it and os the reserved ID just drop out.

This solution works well but is not best kind of.
Following extra things must be included in final solution.
-Must reserve for each ID set area (ID1=1000-10 000, ID2=10 001-20 000 ...)
-How to "reset" tables when limit is on.

Nowadays i use relationships and IDENTITY with SEED value. So i don't need care about
IDs anymore.

In MsSql server it is like:
---- MyData table ----
CREATE TABLE dbo.MyData
      (
      ID int NOT NULL,
      Data varchar(500) NOT NULL
      )  ON [PRIMARY]
GO
---- end ----

-----MyObjects table ---
CREATE TABLE dbo.MyObjects
      (
      ID int NOT NULL IDENTITY (1, 1),
      Object_name varchar(10) NOT NULL
      )  ON [PRIMARY]
GO
ALTER TABLE dbo.MyObjects ADD CONSTRAINT
      PK_MyObjects PRIMARY KEY CLUSTERED
      (
      ID
      ) ON [PRIMARY]

GO
---- end ----

---- Relationship ---
ALTER TABLE dbo.MyData ADD CONSTRAINT
      FK_MyData_MyObjects FOREIGN KEY
      (
      ID
      ) REFERENCES dbo.MyObjects
      (
      ID
      ) ON UPDATE CASCADE
       ON DELETE CASCADE
      
GO
--end--

This mean following in the table named MyData is not allowed insert record with that ID which is not added in MyObject table's ID column.

If you want to do this also more better you should use TRIGGERS too. Sorry i'am too busy in this moment at write more about the usage of triggers so i just copy for you this example.

CREATE TRIGGER updEmployeeData
ON employeeData
FOR update AS
/*Check whether columns 2, 3 or 4 has been updated. If any or all of columns 2, 3 or 4 have been changed, create an audit record. The bitmask is: power(2,(2-1))+power(2,(3-1))+power(2,(4-1)) = 14. To check if all columns 2, 3, and 4 are updated, use = 14 in place of >0 (below).*/

   IF (COLUMNS_UPDATED() & 14) > 0
/*Use IF (COLUMNS_UPDATED() & 14) = 14 to see if all of columns 2, 3, and 4 are updated.*/
      BEGIN
-- Audit OLD record.
      INSERT INTO auditEmployeeData
         (audit_log_type,
         audit_emp_id,
         audit_emp_bankAccountNumber,
         audit_emp_salary,
         audit_emp_SSN)
         SELECT 'OLD',
            del.emp_id,
            del.emp_bankAccountNumber,
            del.emp_salary,
            del.emp_SSN
         FROM deleted del

-- Audit NEW record.
      INSERT INTO auditEmployeeData
         (audit_log_type,
         audit_emp_id,
         audit_emp_bankAccountNumber,
         audit_emp_salary,
         audit_emp_SSN)
         SELECT 'NEW',
            ins.emp_id,
            ins.emp_bankAccountNumber,
            ins.emp_salary,
            ins.emp_SSN
         FROM inserted ins
   END
GO

/*Inserting a new employee does not cause the UPDATE trigger to fire.*/
INSERT INTO employeeData
   VALUES ( 101, 'USA-987-01', 23000, 'R-M53550M', N'Mendel', N'Roland', 32)
GO

/*Updating the employee record for employee number 101 to change the salary to 51000 causes the UPDATE trigger to fire and an audit trail to be produced.*/

UPDATE employeeData
   SET emp_salary = 51000
   WHERE emp_id = 101
GO
SELECT * FROM auditEmployeeData
GO

/*Updating the employee record for employee number 101 to change both the bank account number and social security number (SSN) causes the UPDATE trigger to fire and an audit trail to be produced.*/

UPDATE employeeData
   SET emp_bankAccountNumber = '133146A0', emp_SSN = 'R-M53550M'
   WHERE emp_id = 101
GO

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Add mobile access to browser application 3 131
Sql how to exclude from a table 3 73
mergeTwo  challenge 13 72
find a node in VST 2 47
A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now