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

Stored Procedure Return value question

Hi,

I'm trying to add some data on a company to an SQL server (which I can do),
and want to return the FirmID (SQL identity) of the data that was just
inserted so I can use it to redirect to another page based on the new data -
which is the bit I can't do!

Can anyone point me in the right direction?

Thanks
Nov 19 '05 #1
8 1276
Nick wrote:
Hi,

I'm trying to add some data on a company to an SQL server (which I can do),
and want to return the FirmID (SQL identity) of the data that was just
inserted so I can use it to redirect to another page based on the new data -
which is the bit I can't do!

Can anyone point me in the right direction?


The FirmID is already the return value of the proc?

In that case just add a parameter with Direction.ReturnValue to the
parameter list of your command:

Dim myParameter As New SqlParameter()
myParameter.Direction = ParameterDirection.ReturnValue

myParameter.Value will then give you the value after the execution of
the command.

Daniel

Nov 19 '05 #2
What bit can't you do? Get id back in SQL? Pass result from sp? What is sp
for? Are you aware of SCOPE_IDENTITY?

Eliyahu

"Nick" <Ni**@NTWorks.no.spam.fsnet.co.uk> wrote in message
news:eA*************@TK2MSFTNGP15.phx.gbl...
Hi,

I'm trying to add some data on a company to an SQL server (which I can do), and want to return the FirmID (SQL identity) of the data that was just
inserted so I can use it to redirect to another page based on the new data - which is the bit I can't do!

Can anyone point me in the right direction?

Thanks

Nov 19 '05 #3
Hi,

I have 'Return @@Identity' in the sp - so I think the return value is there.
I'm not sure how to use the parameters though - this is how I'm running the
sp:

Dim strConn As String = "server=" & Global.ServerName & ";database=" &
Global.DatabaseName & ";integrated security=true"

Dim objConn As New SqlConnection(strConn)

Dim objDS As New DataSet

Dim SQLInsert As String

Dim daInsertCompany As New SqlDataAdapter("spd_InsertCompany " & SQLInsert,
objConn)

daUpdateCompany.Fill(objDS)

objDS.Dispose()

daInsertCompany.Dispose()

objConn.Close()

objConn.Dispose()
Where would I add the parameters part? (Or am I running the sp incorrectly
in the first place - although it does work...)

Thanks,

Nick
"Daniel Buchholz" <ne**************@paranor.de> wrote in message
news:37*************@individual.net...
Nick wrote:
Hi,

I'm trying to add some data on a company to an SQL server (which I can
do), and want to return the FirmID (SQL identity) of the data that was
just inserted so I can use it to redirect to another page based on the
new data - which is the bit I can't do!

Can anyone point me in the right direction?


The FirmID is already the return value of the proc?

In that case just add a parameter with Direction.ReturnValue to the
parameter list of your command:

Dim myParameter As New SqlParameter()
myParameter.Direction = ParameterDirection.ReturnValue

myParameter.Value will then give you the value after the execution of the
command.

Daniel

Nov 19 '05 #4
Nick wrote:
Hi,
Where would I add the parameters part? (Or am I running the sp incorrectly
in the first place - although it does work...)


Looks quite complicated.

Perhaps try something like:

Dim strConn As String = "server=" & Global.ServerName & ";database=" &
Global.DatabaseName & ";integrated security=true"

Dim objConn As New SqlConnection(strConn)
objConn.Open()

Dim objCmd As New SqlCommand("spd_InsertCompany " & SQLInsert,
objConn)

objCmd.CommandType = CommandType.StoredProcedure

Dim para As New SqlParameter()
para.Direction = ParameterDirection.ReturnValue

objCmd.Parameters.Add(para)

objCmd.ExecuteNonQuery()

objConn.Close()

--

para.Value should have your value then.

Daniel

Nov 19 '05 #5
Thanks for this. Am I missing something though? The code generates an error
at the ExecuteNonQuery line - saying it can't find the sp... Here's the code
now:

Dim objCmd As New SqlCommand("spd_InsertCompanyDetails " & SQLinsert,
objConn)

objCmd.CommandType = CommandType.StoredProcedure

Dim ReturnIdentity As New SqlParameter

ReturnIdentity.Direction = ParameterDirection.ReturnValue

objCmd.Parameters.Add(ReturnIdentity)

objCmd.ExecuteNonQuery() '<==== Breaks here for the error...

ReturnValue = ReturnIdentity.Value

objConn.Close()
Thanks,

Nick
"Daniel Buchholz" <ne**************@paranor.de> wrote in message
news:37*************@individual.net...
Nick wrote:
Hi,
Where would I add the parameters part? (Or am I running the sp
incorrectly in the first place - although it does work...)


Looks quite complicated.

Perhaps try something like:

Dim strConn As String = "server=" & Global.ServerName & ";database=" &
Global.DatabaseName & ";integrated security=true"

Dim objConn As New SqlConnection(strConn)
objConn.Open()

Dim objCmd As New SqlCommand("spd_InsertCompany " & SQLInsert,
objConn)

objCmd.CommandType = CommandType.StoredProcedure

Dim para As New SqlParameter()
para.Direction = ParameterDirection.ReturnValue

objCmd.Parameters.Add(para)

objCmd.ExecuteNonQuery()

objConn.Close()

--

para.Value should have your value then.

Daniel

Nov 19 '05 #6
Nick wrote:
Thanks for this. Am I missing something though? The code generates an error
at the ExecuteNonQuery line - saying it can't find the sp... Here's the code
now:

Dim objCmd As New SqlCommand("spd_InsertCompanyDetails " & SQLinsert,
objConn)


Uh, sorry, my fault.

You have to specify your parameters to the procedure. The "SQLInsert"
part is not correct.

For each parameter you have to pass do something like

sqlCmd.Parameters.Add("[yourSPParam]", [yourParamValue])

after creating the Command like

Dim objCmd As New SqlCommand("spd_InsertCompanyDetails")

Daniel

Nov 19 '05 #7
Ok - Ignore that last message!

I changed the CommandType to text because I was passing the sp parameters in
the same string, whereas the system was looking for an sp with the name of
the sp+the parameters.

Unfortunately, the parameter value stays 0 rather than the actual identity
value... How do I check it is actually being returned from the sp?

Nick

"Daniel Buchholz" <ne**************@paranor.de> wrote in message
news:37*************@individual.net...
Nick wrote:
Hi,
Where would I add the parameters part? (Or am I running the sp
incorrectly in the first place - although it does work...)


Looks quite complicated.

Perhaps try something like:

Dim strConn As String = "server=" & Global.ServerName & ";database=" &
Global.DatabaseName & ";integrated security=true"

Dim objConn As New SqlConnection(strConn)
objConn.Open()

Dim objCmd As New SqlCommand("spd_InsertCompany " & SQLInsert,
objConn)

objCmd.CommandType = CommandType.StoredProcedure

Dim para As New SqlParameter()
para.Direction = ParameterDirection.ReturnValue

objCmd.Parameters.Add(para)

objCmd.ExecuteNonQuery()

objConn.Close()

--

para.Value should have your value then.

Daniel

Nov 19 '05 #8
Ignore the last post again!!

Return value doesn't work with a commandtype as text...I changed back to
storedprocedure and added the parameters separately, and all seems to be
working.

Thanks very much for your time & patience!!

Nick

"Nick" <Ni**@NTWorks.no.spam.fsnet.co.uk> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Ok - Ignore that last message!

I changed the CommandType to text because I was passing the sp parameters
in the same string, whereas the system was looking for an sp with the name
of the sp+the parameters.

Unfortunately, the parameter value stays 0 rather than the actual identity
value... How do I check it is actually being returned from the sp?

Nick

"Daniel Buchholz" <ne**************@paranor.de> wrote in message
news:37*************@individual.net...
Nick wrote:
Hi,
Where would I add the parameters part? (Or am I running the sp
incorrectly in the first place - although it does work...)


Looks quite complicated.

Perhaps try something like:

Dim strConn As String = "server=" & Global.ServerName & ";database=" &
Global.DatabaseName & ";integrated security=true"

Dim objConn As New SqlConnection(strConn)
objConn.Open()

Dim objCmd As New SqlCommand("spd_InsertCompany " & SQLInsert,
objConn)

objCmd.CommandType = CommandType.StoredProcedure

Dim para As New SqlParameter()
para.Direction = ParameterDirection.ReturnValue

objCmd.Parameters.Add(para)

objCmd.ExecuteNonQuery()

objConn.Close()

--

para.Value should have your value then.

Daniel


Nov 19 '05 #9

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

Similar topics

8
by: C Kirby | last post by:
In SQL Server 2000, I've got a rather lengthy stored procedure, which creates a lot of temporary tables as it processes down through a few sets of data. When testing it through Query Analyzer, it...
15
by: Jarrod Morrison | last post by:
Hi All Im generally a vb programmer and am used to referencing multiple records returned from a query performed on an sql database and im trying to move some functions of my software into sql...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
2
by: Daniel | last post by:
hi ng, i am newbie to sqlserver and my problem seems simple, but i didn't find information about it: How can i display the RETURN @x value of a stored procedure in the sql analyzer of the...
4
by: laurenq uantrell | last post by:
I need to get the value of an output parameter back into my VBA function calling a stored procedure. I'm using the following construction to append a new record in a SQL Server table: ...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
5
by: Sandy | last post by:
Hello - I need a good example of how to take a return value from a stored procedure and use it in vb code. I have an app that searches a database by city and state. If a user makes a typo, the...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
2
by: philip | last post by:
hello, i am new to asp.net and sql server, and i have 3 questions for asking: 1. i am writing a store procedure of login validation for my asp.net application and wondering what the different...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
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
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
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.