Solved

Get index of newly inserted data adapter data set

Posted on 2004-03-25
4
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Simple Linear Regression
Progress

626 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