Connecting Tech Pros Worldwide Forums | Help | Site Map

Return Record ID of Newly Added Record

Tony Stoker
Guest
 
Posts: n/a
#1: Nov 22 '05
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?

Matt
Guest
 
Posts: n/a
#2: Nov 22 '05

re: Return Record ID of Newly Added Record


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" <anonymous@discussions.microsoft.com> wrote in message
news:0e7e01c3aebf$98f55f70$a501280a@phx.gbl...[color=blue]
> 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?[/color]


Matt
Guest
 
Posts: n/a
#3: Nov 22 '05

re: Return Record ID of Newly Added Record


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" <anonymous@discussions.microsoft.com> wrote in message
news:0e7e01c3aebf$98f55f70$a501280a@phx.gbl...[color=blue]
> 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?[/color]


Michael Giagnocavo [MVP]
Guest
 
Posts: n/a
#4: Nov 22 '05

re: Return Record ID of Newly Added Record


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" <anonymous@discussions.microsoft.com> wrote in message
news:0e7e01c3aebf$98f55f70$a501280a@phx.gbl...[color=blue]
> 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?[/color]


Guest
 
Posts: n/a
#5: Nov 22 '05

re: Return Record ID of Newly Added Record


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
[color=blue]
>-----Original Message-----
>In the command that inserts, after the insert, you can[/color]
do SELECT @@IDENTITY[color=blue]
>and that will give you the ID of the last row inserted.
>
>-mike
>MVP
>
>"Tony Stoker" <anonymous@discussions.microsoft.com>[/color]
wrote in message[color=blue]
>news:0e7e01c3aebf$98f55f70$a501280a@phx.gbl...[color=green]
>> 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![/color][/color]
The[color=blue][color=green]
>> recordID is a AutoNumber in the SQL server...
>>
>> How do I return the recordID after I have added the
>> record?[/color]
>
>
>.
>[/color]
Michael Giagnocavo [MVP]
Guest
 
Posts: n/a
#6: Nov 22 '05

re: Return Record ID of Newly Added Record


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

<anonymous@discussions.microsoft.com> wrote in message
news:047f01c3b07e$9651b190$a301280a@phx.gbl...[color=blue]
> 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
>[color=green]
> >-----Original Message-----
> >In the command that inserts, after the insert, you can[/color]
> do SELECT @@IDENTITY[color=green]
> >and that will give you the ID of the last row inserted.
> >
> >-mike
> >MVP
> >
> >"Tony Stoker" <anonymous@discussions.microsoft.com>[/color]
> wrote in message[color=green]
> >news:0e7e01c3aebf$98f55f70$a501280a@phx.gbl...[color=darkred]
> >> 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![/color][/color]
> The[color=green][color=darkred]
> >> recordID is a AutoNumber in the SQL server...
> >>
> >> How do I return the recordID after I have added the
> >> record?[/color]
> >
> >
> >.
> >[/color][/color]


Guest
 
Posts: n/a
#7: Nov 22 '05

re: Return Record ID of Newly Added Record


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
******[color=blue]
>-----Original Message-----
>Have you added an output parameter and assigned[/color]
@@IDENTITY to it? I think[color=blue]
>that should do it. Alternatively, you could use[/color]
ExecuteScalar and get the[color=blue]
>only SELECTed row, @@IDENTITY.
>-mike
>MVP
>
><anonymous@discussions.microsoft.com> wrote in message
>news:047f01c3b07e$9651b190$a301280a@phx.gbl...[color=green]
>> Thanks but I still have a question..I already had the[/color][/color]
sql[color=blue][color=green]
>> part that wasn't the issue but when I click on the[/color][/color]
button[color=blue][color=green]
>> to submit the link how do I assign the newly added[/color][/color]
record[color=blue][color=green]
>> 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
>>[color=darkred]
>> >-----Original Message-----
>> >In the command that inserts, after the insert, you can[/color]
>> do SELECT @@IDENTITY[color=darkred]
>> >and that will give you the ID of the last row[/color][/color][/color]
inserted.[color=blue][color=green][color=darkred]
>> >
>> >-mike
>> >MVP
>> >
>> >"Tony Stoker" <anonymous@discussions.microsoft.com>[/color]
>> wrote in message[color=darkred]
>> >news:0e7e01c3aebf$98f55f70$a501280a@phx.gbl...
>> >> I have a .Net web app that adds a record to a SQL
>> >> database. After the user adds their record I want[/color][/color][/color]
to[color=blue][color=green][color=darkred]
>> >> have a link that will link them to their new record![/color]
>> The[color=darkred]
>> >> recordID is a AutoNumber in the SQL server...
>> >>
>> >> How do I return the recordID after I have added the
>> >> record?
>> >
>> >
>> >.
>> >[/color][/color]
>
>
>.
>[/color]
Michael Giagnocavo [MVP]
Guest
 
Posts: n/a
#8: Nov 22 '05

re: Return Record ID of Newly Added Record


Hi there,
[color=blue]
> 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;"[/color]

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;
[color=blue]
> conSCGC.Open()
> Try
> cmd.ExecuteScalar()
> NewID = cmd.ExecuteScalar("myID")[/color]
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).
[color=blue]
> Response.Write(NewID)
> Catch
> Response.Write(sql)
> Finally
> conSCGC.Close()[/color]

Forgot to dispose your command (SqlCommand implements IDisposable).
[color=blue]
> End Try[/color]

-mike
MVP
[color=blue]
> End Sub
> ******[color=green]
> >-----Original Message-----
> >Have you added an output parameter and assigned[/color]
> @@IDENTITY to it? I think[color=green]
> >that should do it. Alternatively, you could use[/color]
> ExecuteScalar and get the[color=green]
> >only SELECTed row, @@IDENTITY.
> >-mike
> >MVP
> >
> ><anonymous@discussions.microsoft.com> wrote in message
> >news:047f01c3b07e$9651b190$a301280a@phx.gbl...[color=darkred]
> >> Thanks but I still have a question..I already had the[/color][/color]
> sql[color=green][color=darkred]
> >> part that wasn't the issue but when I click on the[/color][/color]
> button[color=green][color=darkred]
> >> to submit the link how do I assign the newly added[/color][/color]
> record[color=green][color=darkred]
> >> 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[/color][/color]
> inserted.[color=green][color=darkred]
> >> >
> >> >-mike
> >> >MVP
> >> >
> >> >"Tony Stoker" <anonymous@discussions.microsoft.com>
> >> wrote in message
> >> >news:0e7e01c3aebf$98f55f70$a501280a@phx.gbl...
> >> >> I have a .Net web app that adds a record to a SQL
> >> >> database. After the user adds their record I want[/color][/color]
> to[color=green][color=darkred]
> >> >> 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?
> >> >
> >> >
> >> >.
> >> >[/color]
> >
> >
> >.
> >[/color][/color]


Closed Thread