Solved

Get index of newly inserted data adapter data set

Posted on 2004-03-25
4
384 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This article will show, step by step, how to integrate R code into a R Sweave document
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

808 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