473,503 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2357
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
389
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
2323
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
2729
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
2176
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
1107
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
2461
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
2099
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
12657
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
1589
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
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
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...
1
6984
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
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.