Q: fetch identity field

G

Guest

Hello,

Let say myTable has two fields: ID and Name. ID is an identity field and
increased sequentially by 1 each time a record is entered into table.


dr["Name"]=â€MayNameâ€;
dataSet11.Tables[myTable].Rows.Add(dr);
sqlDataAdapter1.Update(dataSet11, myTable);

Now after update, I need to find what the ID value is for the record I just
added. This is a multi-user environment. How should I fetch it?

Thanks,
 
G

Guest

Eliyahu,
Thanks for your help.
I found this:

Dim myconn As New SqlConnection("yourconnectionstring")
strsql = "INSERT INTO TABLENAME (COLUMN) VALUES ('" &somevalue &"'); SELECT
SCOPE_IDENTITY() AS LAST_INSERT "
myconn.Open()
Dim mycommand As New SqlCommand(strsql, myconn)
Dim lastinsert As Integer = CType(mycommand.ExecuteScalar(), Integer)
myconn.Close()

Any idea how I can convert this so that it will work with my
sqlDataAdapter1.Update(dataSet11, myTable); command?


Eliyahu Goldin said:
Make a batch and use SCOPE_IDENTITY(). Look up Transact SQL help for
details.

Eliyahu

JIM.H. said:
Hello,

Let say myTable has two fields: ID and Name. ID is an identity field and
increased sequentially by 1 each time a record is entered into table.


dr["Name"]="MayName";
dataSet11.Tables[myTable].Rows.Add(dr);
sqlDataAdapter1.Update(dataSet11, myTable);

Now after update, I need to find what the ID value is for the record I just
added. This is a multi-user environment. How should I fetch it?

Thanks,
 
E

Eliyahu Goldin

Make a batch and use SCOPE_IDENTITY(). Look up Transact SQL help for
details.

Eliyahu
 
P

Paul Clement

¤ Hello,
¤
¤ Let say myTable has two fields: ID and Name. ID is an identity field and
¤ increased sequentially by 1 each time a record is entered into table.
¤
¤
¤ dr["Name"]=”MayName”;
¤ dataSet11.Tables[myTable].Rows.Add(dr);
¤ sqlDataAdapter1.Update(dataSet11, myTable);
¤
¤ Now after update, I need to find what the ID value is for the record I just
¤ added. This is a multi-user environment. How should I fetch it?

See the following:

HOW TO: Retrieve an Identity Value from a Newly Inserted Record from SQL Server by Using Visual
Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;320141


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top