473,396 Members | 1,891 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,396 software developers and data experts.

Return Record ID of Newly Added Record

I have a .Net web app that adds a record to a SQL
database. After the user adds their record I want to
have a link that will link them to their new record! The
recordID is a AutoNumber in the SQL server...

How do I return the recordID after I have added the
record?
Jul 21 '05 #1
6 2348
Use a DataSet and SqlDataAdapter to add the record.

If you create a connection to the database with your server explorer you can
simply drag the table onto your designer and it will create it for you.
Then right click on the adapter and choose Generate dataset.

In your code you simply add the record(look at documention for this, its
really simple)

after you add it the dataset will contain the new record with the AutoNumber
in it.

"Tony Stoker" <an*******@discussions.microsoft.com> wrote in message
news:0e****************************@phx.gbl...
I have a .Net web app that adds a record to a SQL
database. After the user adds their record I want to
have a link that will link them to their new record! The
recordID is a AutoNumber in the SQL server...

How do I return the recordID after I have added the
record?

Jul 21 '05 #2
In the command that inserts, after the insert, you can do SELECT @@IDENTITY
and that will give you the ID of the last row inserted.

-mike
MVP

"Tony Stoker" <an*******@discussions.microsoft.com> wrote in message
news:0e****************************@phx.gbl...
I have a .Net web app that adds a record to a SQL
database. After the user adds their record I want to
have a link that will link them to their new record! The
recordID is a AutoNumber in the SQL server...

How do I return the recordID after I have added the
record?

Jul 21 '05 #3
Thanks but I still have a question..I already had the sql
part that wasn't the issue but when I click on the button
to submit the link how do I assign the newly added record
id to a variable so I can then display a confirm page
with a link to the users newly added record page? I
right now am using the executeNonQuery but it just adds
the record and doesn't return any value.

Thanks
-----Original Message-----
In the command that inserts, after the insert, you can do SELECT @@IDENTITYand that will give you the ID of the last row inserted.

-mike
MVP

"Tony Stoker" <an*******@discussions.microsoft.com> wrote in messagenews:0e****************************@phx.gbl...
I have a .Net web app that adds a record to a SQL
database. After the user adds their record I want to
have a link that will link them to their new record! The recordID is a AutoNumber in the SQL server...

How do I return the recordID after I have added the
record?

.

Jul 21 '05 #4
Have you added an output parameter and assigned @@IDENTITY to it? I think
that should do it. Alternatively, you could use ExecuteScalar and get the
only SELECTed row, @@IDENTITY.
-mike
MVP

<an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
Thanks but I still have a question..I already had the sql
part that wasn't the issue but when I click on the button
to submit the link how do I assign the newly added record
id to a variable so I can then display a confirm page
with a link to the users newly added record page? I
right now am using the executeNonQuery but it just adds
the record and doesn't return any value.

Thanks
-----Original Message-----
In the command that inserts, after the insert, you can

do SELECT @@IDENTITY
and that will give you the ID of the last row inserted.

-mike
MVP

"Tony Stoker" <an*******@discussions.microsoft.com>

wrote in message
news:0e****************************@phx.gbl...
I have a .Net web app that adds a record to a SQL
database. After the user adds their record I want to
have a link that will link them to their new record! The recordID is a AutoNumber in the SQL server...

How do I return the recordID after I have added the
record?

.

Jul 21 '05 #5
Mike-
Thanks for writing back and don't laugh at what I am
about to post I am new to this type of stuff....this is
what I have tried and it isn't working(please note in the
try section I was just trying to see if it was grabbing
my variable or not any suggestions or hints would be
greatly appreciated:

********
Private Sub AddArticle()

Dim sql As String
Dim cmd As SqlCommand
Dim sb As StringBuilder
Dim Values As ArrayList

Dim fNewsDate As String
Dim fNewsSubBy As String
Dim fNewsHdr As String
Dim fNewsDesc As String
Dim NewID
fNewsDate = "'" & txtNewsDate.Text & "',"
fNewsSubBy = "'" & txtNewsSubBy.Text & "',"
fNewsHdr = "'" & txtNewsHdr.Text & "',"
fNewsDesc = "'" & txtNewsDesc.Text & "'"

sql = "SET NOCOUNT ON; Insert INTO [News]
(NewsDate,NewsSubBy,NewsHdr,NewsDesc) Values " & "(" &
fNewsDate & fNewsSubBy & fNewsHdr & fNewsDesc & ");SELECT
@@IDENTITY AS myID FROM News;"

cmd = New SqlCommand(sql, conSCGC)
conSCGC.Open()

Try
cmd.ExecuteScalar()

NewID = cmd.ExecuteScalar("myID")
Response.Write(NewID)
Catch
Response.Write(sql)
Finally
conSCGC.Close()
End Try

End Sub
******
-----Original Message-----
Have you added an output parameter and assigned @@IDENTITY to it? I thinkthat should do it. Alternatively, you could use ExecuteScalar and get theonly SELECTed row, @@IDENTITY.
-mike
MVP

<an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
Thanks but I still have a question..I already had the sql part that wasn't the issue but when I click on the button to submit the link how do I assign the newly added record id to a variable so I can then display a confirm page
with a link to the users newly added record page? I
right now am using the executeNonQuery but it just adds
the record and doesn't return any value.

Thanks
>-----Original Message-----
>In the command that inserts, after the insert, you can

do SELECT @@IDENTITY
>and that will give you the ID of the last row inserted. >
>-mike
>MVP
>
>"Tony Stoker" <an*******@discussions.microsoft.com>

wrote in message
>news:0e****************************@phx.gbl...
>> I have a .Net web app that adds a record to a SQL
>> database. After the user adds their record I want to >> have a link that will link them to their new record!

The
>> recordID is a AutoNumber in the SQL server...
>>
>> How do I return the recordID after I have added the
>> record?
>
>
>.
>

.

Jul 21 '05 #6
Hi there,
fNewsDate = "'" & txtNewsDate.Text & "',"
fNewsSubBy = "'" & txtNewsSubBy.Text & "',"
fNewsHdr = "'" & txtNewsHdr.Text & "',"
fNewsDesc = "'" & txtNewsDesc.Text & "'"

sql = "SET NOCOUNT ON; Insert INTO [News]
(NewsDate,NewsSubBy,NewsHdr,NewsDesc) Values " & "(" &
fNewsDate & fNewsSubBy & fNewsHdr & fNewsDesc & ");SELECT
@@IDENTITY AS myID FROM News;"
Few points. Never concat an SQL string like that. The code below has
security problems. Always use parameters.

Try this:

sql = "SET NOCOUNT ON; INSERT [News] (NewsDate, ETC ) VALUES (@NewsDate,
@ETC); SET @ID = SELECT @@IDENTITY;";
new command, bla bla

myCommand.Add("@NewDate", SqlDbTypes.DateTime).Value = myDateTime;
myCommand.Add("@ETC", SqlDbTypes.Whatever).Value = ETC;
myCommand.Add("@ID", SqlDbTypes.Int).Direction =
ParameterDirection.Output;
conSCGC.Open()
Try
cmd.ExecuteScalar()
NewID = cmd.ExecuteScalar("myID") Why call ExecuteScalar() twice? Also, shouldn't you have to cast there?
You DO have OPTION STRICT ON, right? :)

Did this work the way you had it? Try with the output param if not. You
may have to play around a bit (I use stored procedures for inserts, so my
syntax might be off somewhere).
Response.Write(NewID)
Catch
Response.Write(sql)
Finally
conSCGC.Close()
Forgot to dispose your command (SqlCommand implements IDisposable).
End Try
-mike
MVP
End Sub
******
-----Original Message-----
Have you added an output parameter and assigned

@@IDENTITY to it? I think
that should do it. Alternatively, you could use

ExecuteScalar and get the
only SELECTed row, @@IDENTITY.
-mike
MVP

<an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
Thanks but I still have a question..I already had the sql part that wasn't the issue but when I click on the button to submit the link how do I assign the newly added record id to a variable so I can then display a confirm page
with a link to the users newly added record page? I
right now am using the executeNonQuery but it just adds
the record and doesn't return any value.

Thanks

>-----Original Message-----
>In the command that inserts, after the insert, you can
do SELECT @@IDENTITY
>and that will give you the ID of the last row inserted. >
>-mike
>MVP
>
>"Tony Stoker" <an*******@discussions.microsoft.com>
wrote in message
>news:0e****************************@phx.gbl...
>> I have a .Net web app that adds a record to a SQL
>> database. After the user adds their record I want to >> have a link that will link them to their new record!
The
>> recordID is a AutoNumber in the SQL server...
>>
>> How do I return the recordID after I have added the
>> record?
>
>
>.
>

.

Jul 21 '05 #7

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

Similar topics

7
by: Tony Stoker | last post by:
I have a .Net web app that adds a record to a SQL database. After the user adds their record I want to have a link that will link them to their new record! The recordID is a AutoNumber in the...
8
by: MLH | last post by:
Here's a snippet from A97 HELP on AddNew... The record that was current before you used AddNew remains current. If you want to make the new record current, you can set the Bookmark property to...
6
by: AA Arens | last post by:
Hi, I have a database with 2 main forms. Contacts and companies. I share the base with two others via LAN. On the companies form I have buttons to navigate throught the records (>400). We are...
2
by: Kaur | last post by:
Hi, I am working in MS Access 2000 and have created two forms. Form 1 is called frmParent (which has a subform called SfrmChild). FrmParent has a list box that lists all the Last Names of...
0
by: Sooraj | last post by:
Hi all I am using vb.net 2002. To add records to grid, I click on button and display modal form with various text boxes. f.showdialog() When I save the record (by selecting MAXID from table)...
9
by: =?Utf-8?B?UHJhdmlu?= | last post by:
We are using .net Framework 1.1 We are having one page on which we are using this Grid component. From this page we open a popup for adding new record as well as for editing an existing record...
2
by: MLH | last post by:
Have a data entry form bound to a table. Has a save button on it that launches DoCmd.RunCommand acCmdSaveRecord in Access 97. In the form after-update code, I have a line looking something like...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
0
by: fig000 | last post by:
Hi, I'm using an objectdatasource. The insert procedure called by the objectdatasource is in a separate library file outside of the aspx and the codebehind that is using the objectdatasource in...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.