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

Stored Procedures, DAL & Returning Values

Hello,

We have a stored procedure that does a basic insert of values. I am
then able to retrieve the ID number created for this new record. We are
currently using ASP.NET 2.0 and use N-Tier Architecture.

The Stored Procedures are used through TableAdaptors, which in turn are
used by Class Files.

I wish to be able to return this new ID value using the Stored
Procedure, TableAdaptor and for it to be available in the Class file.
At present we are unable to get this working. Does anybody have
experience of getting this method of operation working at all?

An Example Stored Procedure:
--------------------------------------------
CREATE PROCEDURE [dbo].[InsertSupplier]
(
@Company nvarchar(1000),
@SupplierId int OUTPUT
)

AS
BEGIN
/* Insert Supplier */
INSERT INTO Supplier (Company)
VALUES (@Company)

/* Retrieve Supplier Id */
SET @SupplierId=@@IDENTITY
END
GO
--------------------------------------------

An Example Portion of The Class (VB.NET 2.0):

--------------------------------------------
'*** Create New Supplier
Public Function CreateSupplier( _
ByVal Company As String, _
ByVal SupplierId As Integer) As Boolean

'*** Declare Variables
Dim Result As Boolean

'*** Insert Supplier
Result = Adapter.Insert(Company, SupplierId)

'*** Publish Result
Return Result

End Function
--------------------------------------------

As you can see from the class, this deals wwith actually enterting the
values into the stored procedure, which will then add it into the table
itself. How would I be able to represent the returning value in this
portion of the class?

I would like to be able to do this without having to do another call to
the database. I come from ASP 3.0 (Classic) background and am used to
using Stored Procedures with Command objects that will allow the
returning of values. I have as yet, been unable to simulate such
operations using ASP.NET 2.0 and am looking for help with this
situation.

Anyone have any ideas, solutions?
Sincerely,

SP

Nov 1 '06 #1
4 4329
Hello,
>
We have a stored procedure that does a basic insert of values. I am
then able to retrieve the ID number created for this new record. We are
currently using ASP.NET 2.0 and use N-Tier Architecture.

The Stored Procedures are used through TableAdaptors, which in turn are
used by Class Files.

I wish to be able to return this new ID value using the Stored
Procedure, TableAdaptor and for it to be available in the Class file.
At present we are unable to get this working. Does anybody have
experience of getting this method of operation working at all?

An Example Stored Procedure:
--------------------------------------------
CREATE PROCEDURE [dbo].[InsertSupplier]
(
@Company nvarchar(1000),
@SupplierId int OUTPUT
)

AS
BEGIN
/* Insert Supplier */
INSERT INTO Supplier (Company)
VALUES (@Company)

/* Retrieve Supplier Id */
SET @SupplierId=@@IDENTITY
END
GO
--------------------------------------------

An Example Portion of The Class (VB.NET 2.0):

--------------------------------------------
'*** Create New Supplier
Public Function CreateSupplier( _
ByVal Company As String, _
ByVal SupplierId As Integer) As Boolean

'*** Declare Variables
Dim Result As Boolean

'*** Insert Supplier
Result = Adapter.Insert(Company, SupplierId)

'*** Publish Result
Return Result

End Function
--------------------------------------------

As you can see from the class, this deals wwith actually enterting the
values into the stored procedure, which will then add it into the table
itself. How would I be able to represent the returning value in this
portion of the class?

I would like to be able to do this without having to do another call to
the database. I come from ASP 3.0 (Classic) background and am used to
using Stored Procedures with Command objects that will allow the
returning of values. I have as yet, been unable to simulate such
operations using ASP.NET 2.0 and am looking for help with this
situation.

Anyone have any ideas, solutions?

Sincerely,

SP
What is that Adapter you use?

To call a stored procedure and use the returned parameters, I use
something like:
- create a SqlCommand object
- set the CommandText property to the name of the stored procedure to
call and the CommandType to CommandType.StoredProcedure)
- add parameters. Two in your case: @Company is an input param,
@SuppliedId is output (or in/out).
- set the Connection property
- open the connection
- Execute the command (ExecuteNonQuery method on the command in your
case)
- close the connection (you might want to put the execute in a
"try"-block and the connection-close in the "finally")
- read the value from the @SupplierId parameter.

this works in 1.1 and I don't expect that to be changed (much) in 2.0

Hans Kesting
Nov 1 '06 #2
I created an example that should help. Note everything is coded in the code
behing, but logic should be separated from the presentation.

-- begin vb.net suxx code --

Partial Class _Default
Inherits System.Web.UI.Page

Private Const ConnectionString As String =
"server=serverName;uid=UserName;password=Password; database=DatebaseName"

Public Class Supplier

Private _id As Integer
Private _company As String

Public Sub New()
End Sub

Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
If (value < 0) Then
Throw New ArgumentOutOfRangeException()
End If
_id = value
End Set
End Property

Public Property Company() As String
Get
Return _company
End Get
Set(ByVal value As String)
_company = value
End Set
End Property

End Class

Private Sub InsertSupplier(ByVal supplier As Supplier)

Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("InsertSupplier", connection)

command.Parameters.Add("@Company", SqlDbType.NVarChar).Value =
supplier.Company
command.Parameters.Add("@SupplierId", SqlDbType.Int).Direction =
ParameterDirection.Output
command.CommandType = CommandType.StoredProcedure

Try

connection.Open()
command.ExecuteNonQuery()

Catch ex As Exception
Throw ex
Finally
connection.Close()
End Try

supplier.Id = CType(command.Parameters("@SupplierId").Value, Integer)

End Sub

Protected Sub InsertBUtton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles InsertBUtton.Click

Dim supplier As New Supplier

supplier.Company = txtCompany.Text

InsertSupplier(supplier)
Response.Write("inserted. id = " & supplier.Id.ToString())

End Sub
End Class
-- end code --

-- begin T-SQL code --

CREATE PROCEDURE [dbo].[InsertSupplier]
@Company nvarchar(1000),
@SupplierId int OUTPUT
AS
BEGIN
/* Insert Supplier */
INSERT INTO Supplier (Company)
VALUES (@Company)

/* Retrieve Supplier Id */
SET @SupplierId = SCOPE_IDENTITY()
END
GO

-- end code

--
Milosz Skalecki
MCAD
"scparker" wrote:
Hello,

We have a stored procedure that does a basic insert of values. I am
then able to retrieve the ID number created for this new record. We are
currently using ASP.NET 2.0 and use N-Tier Architecture.

The Stored Procedures are used through TableAdaptors, which in turn are
used by Class Files.

I wish to be able to return this new ID value using the Stored
Procedure, TableAdaptor and for it to be available in the Class file.
At present we are unable to get this working. Does anybody have
experience of getting this method of operation working at all?

An Example Stored Procedure:
--------------------------------------------
CREATE PROCEDURE [dbo].[InsertSupplier]
(
@Company nvarchar(1000),
@SupplierId int OUTPUT
)

AS
BEGIN
/* Insert Supplier */
INSERT INTO Supplier (Company)
VALUES (@Company)

/* Retrieve Supplier Id */
SET @SupplierId=@@IDENTITY
END
GO
--------------------------------------------

An Example Portion of The Class (VB.NET 2.0):

--------------------------------------------
'*** Create New Supplier
Public Function CreateSupplier( _
ByVal Company As String, _
ByVal SupplierId As Integer) As Boolean

'*** Declare Variables
Dim Result As Boolean

'*** Insert Supplier
Result = Adapter.Insert(Company, SupplierId)

'*** Publish Result
Return Result

End Function
--------------------------------------------

As you can see from the class, this deals wwith actually enterting the
values into the stored procedure, which will then add it into the table
itself. How would I be able to represent the returning value in this
portion of the class?

I would like to be able to do this without having to do another call to
the database. I come from ASP 3.0 (Classic) background and am used to
using Stored Procedures with Command objects that will allow the
returning of values. I have as yet, been unable to simulate such
operations using ASP.NET 2.0 and am looking for help with this
situation.

Anyone have any ideas, solutions?
Sincerely,

SP

Nov 1 '06 #3
Hi,

scparker wrote:
I wish to be able to return this new ID value using the Stored
Procedure, TableAdaptor and for it to be available in the Class file.
At present we are unable to get this working. Does anybody have
experience of getting this method of operation working at all?
FWIW - as an alternative to the regular approaches mentioned in this
thread, you could simply return your new ID within the resultset if your SP
has to return that new ID only, as in:

--- 8< ---
CREATE PROCEDURE [dbo].[InsertSupplier]
(
@Company nvarchar(1000),
@SupplierId int OUTPUT
)

AS
BEGIN
/* Insert Supplier */
INSERT INTO Supplier (Company)
VALUES (@Company)

/* Retrieve Supplier Id */
SELECT @@IDENTITY
END
GO
--- 8< ---

Retrieving your new ID could then be handled using i.e.
intNewID=ctype(cmdSomeCommand.ExecuteScalar,intege r)

Cheers,
Olaf
--
My .02: www.Resources.IntuiDev.com
Nov 1 '06 #4
Hello,

Thank you for your responses. It appears you all do follow the same
line of thinking.

To answer Hans Kesting;
The Adapter is an object created using Visual Studio 2005. It creates
the Data Access Layer (DAL). What it effectively does is bind the four
operations of Select, Insert, Update & Delete into One Object. This
results in the following code:

Adapter.Insert(Company,SupplierId)
or
Adapter.Select(SupplierId)
etc
So, you create a stored procedure that is the definitive Select
statement you are going to use to communicate with that particular
table. Using VS2005 you use the graphical interface to select a
particular stored procedure to assume the Select command. The same goes
for Insert, Update and Delete. The name of these stored procedures
could be anything
(say: CREATE PROCEDURE WeWantToSelectASupplier(@SupplierID as INT) AS
....)
and when you select that to be the defined Select Bind, instead of
calling Adpater.WeWantToSelectASupplier - you call Adapter.Select. I do
not know if 1.1 had this functionality as I have come from Classic ASP
to ASP.NET 2.0 - so I hope this makes sense to you.

To answer Milosz Skalecki;
This really continues with my answer to Hans Kesting. Because of this
use of the TableAdaptor, it appears we can negate the need for the use
of Command and Connection objects. We have learnt that what we are
looking to do is to return a scalar value from a dataset. The following
links may help to elaborate the issue we are having:
http://aspnet.4guysfromrolla.com/articles/062905-1.aspx
The above details very similar to the example you have provided and
then goes on to discuss the return value being used in the stored
procedure. It is this value we wish to extract using the TableAdaptor
objects.

http://blogs.gotdotnet.com/youngjoo/...25/678320.aspx
The link above continues the discussion by showing how to get the
return value by using the scalar extractor to bring the actual value
through. It is however, hard to follow and this is where we have
reached a stumbling block. Are you able to elaborate on what is going
on here, in the context of what we have been talking about?

To answer Olaf Rabbachin;
Thank you for the contribution with regard to the Stored Procedure
change. It makes little difference whether you use @@IDENTITY or
SCOPE_IDENTITY() to retrieve the value required. However, I will be
using the SCOPE_IDENTITY() in future, so thank you for your assistance
with this.

If any of you are able to help with the understanding of using a Scalar
value with TableAdaptors, you will be of great assistance.

Sincerely,

SP

Nov 2 '06 #5

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

Similar topics

0
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi All, I am having problem returning values from a Stored Procedure that creates a dynamic table (table variable) inserts values during a procedure and then I select from that dynamic table to...
2
by: M Wells | last post by:
Hi All, I'm wondering if anyone can tell me if it's possible to search for stored procedures by their contents? I tend to leave very descriptive notes in stored procedures and they're...
9
by: Ralph Cramden | last post by:
I'm writing a VB6 app which calls several stored procedures on my SQL Server DB. The first stored procedure must complete its inserts before the second stored procedure can query the modified...
2
by: Anthony Judd | last post by:
Hi all, Just a quick question. I am using Sql Server 2000 and calling stored procedures from asp. At this stage i am forgoing using the ADO command object and am simply create dynamic like sql...
6
by: Terentius Neo | last post by:
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a select statement? I mean something like this: Select c.number, call procedure( c.number ) as list from table c With best...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
1
by: mandible | last post by:
Hello I was wondering if I"m doing something wrong with using db2advis I have 2 stored procedures are are massive, I created an input file to call both of these stored procedures and I dont' seem...
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....
7
by: eholz1 | last post by:
Hello PHP group, Could someone help me out? I have PHP 5.2, Apache 2.0, and MySQL 5.0 running on Linux (Redhat Fedora Core 6). All that works fine. I would like to be able to "call" a stored...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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,...

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.