473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SELECT @@IDENTITY returns 0 in Transaction

Hi,

Im trying to use SELECT @@IDENTITY in a transaction. But it always returns 0.
Im using an ms access database. And using the following code.
What am i doing wrong?

Best regards banski

public int SavePage(int id....)
{
int SomeId = 0;
string myDNS = ConfigurationSettings.AppSettings["ConnectionString"];
OleDbConnection myConn = new OleDbConnection(myDNS);
myConn.Open();
OleDbTransaction myTrans = myConn.BeginTransaction();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = myConn;
cmd.Transaction = myTrans;
try
{
cmd.CommandText = "InsertDataData";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageTypeId", OleDbType.Integer).Value = PageTypeId;
//adding more parameters
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();

cmd.CommandText = "SELECT @@IDENTITY";
SomeId = (int)cmd.ExecuteScalar();

//Making more inserts using identity
......
......

myTrans.Commit();

}
catch(OleDbException e)
{

Console.WriteLine(e.ToString());

myTrans.Rollback();
}
finally
{
myConn.Close();
}
return SomeId;

}
Nov 19 '05 #1
2 5537
My guess is that you actually have two seperate operations going on. Even
though the transaction is the same it's no longer in the same scope. A scope
has a much shorter lifespan. Your best bet is to actually try to do your
insert operation, or whatever is being performed in the ExecuteNonQuery, and
select the @@Identity value at the same time, otherwise they won't be
considered as being in the same scope.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Banski" <th********@hotmail.com> wrote in message
news:a1**************************@posting.google.c om...
Hi,

Im trying to use SELECT @@IDENTITY in a transaction. But it always returns
0.
Im using an ms access database. And using the following code.
What am i doing wrong?

Best regards banski

public int SavePage(int id....)
{
int SomeId = 0;
string myDNS = ConfigurationSettings.AppSettings["ConnectionString"];
OleDbConnection myConn = new OleDbConnection(myDNS);
myConn.Open();
OleDbTransaction myTrans = myConn.BeginTransaction();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = myConn;
cmd.Transaction = myTrans;
try
{
cmd.CommandText = "InsertDataData";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageTypeId", OleDbType.Integer).Value = PageTypeId;
//adding more parameters
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();

cmd.CommandText = "SELECT @@IDENTITY";
SomeId = (int)cmd.ExecuteScalar();

//Making more inserts using identity
......
......

myTrans.Commit();

}
catch(OleDbException e)
{

Console.WriteLine(e.ToString());

myTrans.Rollback();
}
finally
{
myConn.Close();
}
return SomeId;

}

Nov 19 '05 #2
I recommend you change your insert procedure to return SCOPE_IDENTITY
since @@identity is a global variable and will return the identity of
the last inserted value within your transaction.

In my experience, the return of SCOPE_IDENTITY is not handled
automatically. I needed to cast it to a decimal and then parse back to
an Int32.

Sample SP change:
---
ALTER PROCEDURE InsertDataData
@PageTypeId int
AS
INSERT INTO SampleTable
(
PageTypeId
)
VALUES
(
@PageTypeId
)

--Do other processing...

--Return Inserted Row's ID
SELECT SCOPE_IDENTITY()
---

That would change your ADO code to this:
{
cmd.CommandText = "InsertDataData";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageTypeId", OleDbType.Integer).Value =
PageTypeId;
//adding more parameters
SomeId = decimal.ToInt32((decimal)cmd.ExecuteScalar());

//Making more inserts using identity
.......
.......
myTrans.Commit();
}

- Jon
http://weblogs.asp.net/jgalloway

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: iqbal | last post by:
Hi, I have 2 sql servers (A and B) and have established a link between them using sp_addlinkedserver. I have created a table (test) on sql server A. I have created a view (test) on sql server B...
2
by: Karthik.S | last post by:
Does DB2 have a select @@ identity equivalent. Karthik
1
by: Liming | last post by:
Hello all, I have a need to use "select @@identity" to get the autoid on the row that I just inserted. The insert store procedure executed correctly as if I took out the transaction, i can see...
7
by: Brian | last post by:
Hello all - I am trying to Insert a new record to an Access 2002 database. Following the insert, I need to obtain the primary key of the inserted row (person_ID) which is an Autonumber field. I...
2
by: Beowulf | last post by:
If I run this statement in Query Analyzer, it properly returns 1 for my testing table. But if I put the statement into a stored procedure, the stored procedure returns NULL. What am I doing...
4
by: nsikkandar | last post by:
Greetings, I am in need of getting "Auto Generated Number" of Primary Key from master table (I am, using Access) when I insert a row through ADO.NET from VB.NET. Based on this ID, I want to...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
3
by: cmrhema | last post by:
Hi, I have a table emp1 where i have two fields empno and empname. I have not assigned primary keys to any of these. After inserting values when I put on select @@identity or select @@identity...
6
by: BobRoyAce | last post by:
Let's say that I am performing a bunch of insert/update queries within a transaction that is created as follows: Dim cnn As New SqlClient.SqlConnection(My.Settings.GRPConnectionString)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.