Solved

Get index of newly inserted data adapter data set

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installed softare without GUID 6 65
Microsoft C++ code failing in executable that worked 9 85
C Programming - If Statement 8 72
Recommendation vb6 to vb.net or others 14 109
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

21 Experts available now in Live!

Get 1:1 Help Now