473,398 Members | 2,113 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,398 software developers and data experts.

Stored Proc with parameters

Hi all,

I was wondering if anyone could give me a small example of running a stored
procedure with both output and input parameters via asp.net (vb).

I have tried a couple of things, one of which worked, but now I'm trying to
change my code so that its the 'proper' way of doing things..

My stored proc expects the following:

@ErrorCode int OUTPUT,
@CDSPatientID int OUTPUT,
@OrgID int,
@NHSNumber varchar(7),
@SQLRIID varchar(7),
@HospitalNumber varchar(17),
@HospitalType varchar(3)

and is called writePatientUBHT

The code I initially had was as follows:

' exec stored procedure to add patient details
strSQL = "DECLARE "
strSQL += "@ErrorCode int, @CDSPatientID int "
strSQL += "EXEC writePatientUBHT "
strSQL += "@ErrorCode OUTPUT, @CDSPatientID OUTPUT, "
strSQL += "'" & Session("OrgID") & "', "
strSQL += "'" & m_params.GetParameterByName("NHS Number").m_queryStringData
& "', "
strSQL += "'" & m_params.GetParameterByName("SQLRI ID").m_queryStringData &
"', "
strSQL += "'" & m_params.GetParameterByName("Hospital
Number").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("Hospital").m_queryStr ingData &
"' "
strSQL += "SELECT @ErrorCode AS ErrorCode, @CDSPatientID AS CDSPatientID"

I then have a datareader which reads and set two variables to equal the
values returned from @ErrorCode and @CDSPatientID

I have done a few sql statements where I now add parameters as the values
going in - but trying the same method for getting them out seems a bit more
complex...I was heading down this road when I ran into problems..

' declare variables
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim objReader As SqlDataReader
Dim strSQL As String
Dim intCDSPatientID As Integer
Dim intErrorCode As Integer

' Create and open our database connection
objConnection = New
SqlConnection(ConfigurationSettings.GetConfig("app Settings")("connString"))
objConnection.Open()

objCommand = New SqlCommand
objCommand.CommandType = CommandType.StoredProcedure
objCommand.Connection = objConnection

strSQL += "EXEC writePatientUBHT "
strSQL += "'" & Session("OrgID") & "', "
strSQL += "'" & m_params.GetParameterByName("NHS Number").m_queryStringData
& "', "
strSQL += "'" & m_params.GetParameterByName("SQLRI ID").m_queryStringData &
"', "
strSQL += "'" & m_params.GetParameterByName("Hospital
Number").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("Hospital").m_queryStr ingData &
"' "

' testing the parameters stuff
Dim sqlParameter As SqlParameter
Dim sqlParameter2 As SqlParameter

sqlParameter = New SqlParameter
sqlParameter2 = New SqlParameter

sqlParameter = objCommand.Parameters.Add("@ErrorCode", SqlDbType.Int)
sqlParameter.Direction = ParameterDirection.Output

sqlParameter2 = objCommand.Parameters.Add("@CDSPatientID", SqlDbType.Int)
sqlParameter2.Direction = ParameterDirection.Output

' set our command object
objCommand.CommandText = strSQL

' execute
objReader = objCommand.ExecuteReader()

' Close reader
objReader.Close()

' try to extract parameters
intCDSPatientID = objCommand.Parameters("@CDSPatientID").Value()
intErrorCode = objCommand.Parameters("@ErrorCode").Value()

' then all the tidy up code...
I had assumed (in the example above) that I would now not need to specify
the 'OUTPUT' parameters immediately after the 'EXEC writePatientUBHT', so I
removed those, however, when I ran the code I got an error saying it could
find the stored procedure named 'EXEC writePatientUBHT 'value', 'value',
'value', 'value' - I put this down to the fact that I was now specifying
the commandType as a stored procedure and that somewhere I probably now need
to say storeProc.name = "writePatientUBHT" or something...which would
suggest that I then need to add all my other values as parameters as well
and specify their directions?

Could anyone please advise me on this, and if possible perhaps a small
example using my code (where appropriate) from the above...

Thanks in advance for any help

Regards

Rob
Nov 18 '05 #1
2 1846
Hello Rob...
I dont know whether this will suite u or not.. But there is something knows
as Microsoft. Application Blocks for data which u can download from the
microsoft website. It contains a file called sqlhelper.cs (available in vb
also). It has a lot of static methods which return different objects which
will suite ur requirement. try using that. It reduces the codes that u've to
write......

cheers,
Laiju
"Rob Meade" <ro**********@NOSPAMubht.swest.nhs.uk> wrote in message
news:OD**************@TK2MSFTNGP10.phx.gbl...
Hi all,

I was wondering if anyone could give me a small example of running a stored procedure with both output and input parameters via asp.net (vb).

I have tried a couple of things, one of which worked, but now I'm trying to change my code so that its the 'proper' way of doing things..

My stored proc expects the following:

@ErrorCode int OUTPUT,
@CDSPatientID int OUTPUT,
@OrgID int,
@NHSNumber varchar(7),
@SQLRIID varchar(7),
@HospitalNumber varchar(17),
@HospitalType varchar(3)

and is called writePatientUBHT

The code I initially had was as follows:

' exec stored procedure to add patient details
strSQL = "DECLARE "
strSQL += "@ErrorCode int, @CDSPatientID int "
strSQL += "EXEC writePatientUBHT "
strSQL += "@ErrorCode OUTPUT, @CDSPatientID OUTPUT, "
strSQL += "'" & Session("OrgID") & "', "
strSQL += "'" & m_params.GetParameterByName("NHS Number").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("SQLRI ID").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("Hospital
Number").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("Hospital").m_queryStr ingData & "' "
strSQL += "SELECT @ErrorCode AS ErrorCode, @CDSPatientID AS CDSPatientID"

I then have a datareader which reads and set two variables to equal the
values returned from @ErrorCode and @CDSPatientID

I have done a few sql statements where I now add parameters as the values
going in - but trying the same method for getting them out seems a bit more complex...I was heading down this road when I ran into problems..

' declare variables
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim objReader As SqlDataReader
Dim strSQL As String
Dim intCDSPatientID As Integer
Dim intErrorCode As Integer

' Create and open our database connection
objConnection = New
SqlConnection(ConfigurationSettings.GetConfig("app Settings")("connString")) objConnection.Open()

objCommand = New SqlCommand
objCommand.CommandType = CommandType.StoredProcedure
objCommand.Connection = objConnection

strSQL += "EXEC writePatientUBHT "
strSQL += "'" & Session("OrgID") & "', "
strSQL += "'" & m_params.GetParameterByName("NHS Number").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("SQLRI ID").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("Hospital
Number").m_queryStringData & "', "
strSQL += "'" & m_params.GetParameterByName("Hospital").m_queryStr ingData & "' "

' testing the parameters stuff
Dim sqlParameter As SqlParameter
Dim sqlParameter2 As SqlParameter

sqlParameter = New SqlParameter
sqlParameter2 = New SqlParameter

sqlParameter = objCommand.Parameters.Add("@ErrorCode", SqlDbType.Int)
sqlParameter.Direction = ParameterDirection.Output

sqlParameter2 = objCommand.Parameters.Add("@CDSPatientID", SqlDbType.Int)
sqlParameter2.Direction = ParameterDirection.Output

' set our command object
objCommand.CommandText = strSQL

' execute
objReader = objCommand.ExecuteReader()

' Close reader
objReader.Close()

' try to extract parameters
intCDSPatientID = objCommand.Parameters("@CDSPatientID").Value()
intErrorCode = objCommand.Parameters("@ErrorCode").Value()

' then all the tidy up code...
I had assumed (in the example above) that I would now not need to specify
the 'OUTPUT' parameters immediately after the 'EXEC writePatientUBHT', so I removed those, however, when I ran the code I got an error saying it could
find the stored procedure named 'EXEC writePatientUBHT 'value', 'value',
'value', 'value' - I put this down to the fact that I was now specifying
the commandType as a stored procedure and that somewhere I probably now need to say storeProc.name = "writePatientUBHT" or something...which would
suggest that I then need to add all my other values as parameters as well
and specify their directions?

Could anyone please advise me on this, and if possible perhaps a small
example using my code (where appropriate) from the above...

Thanks in advance for any help

Regards

Rob

Nov 18 '05 #2
The following seems to work nicely :o)

' declare variables
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim objReader As SqlDataReader
Dim strSQL As String
Dim intCDSPatientID As Integer
Dim intErrorCode As Integer

' Create and open our database connection
objConnection = New
SqlConnection(ConfigurationSettings.GetConfig("app Settings")("connString"))
objConnection.Open()
objCommand = New SqlCommand
objCommand.CommandType = CommandType.StoredProcedure
objCommand.Connection = objConnection

strSQL = "writePatientUBHT"

' add parameters
objCommand.Parameters.Add(createParameter("@ErrorC ode", SqlDbType.Int,
ParameterDirection.Output))
objCommand.Parameters.Add(createParameter("@CDSPat ientID", SqlDbType.Int,
ParameterDirection.Output))
objCommand.Parameters.Add(createParameter("@OrgID" , SqlDbType.Int,
ParameterDirection.Input, Session("OrgID")))
objCommand.Parameters.Add(createParameter("@NHSNum ber", SqlDbType.VarChar,
ParameterDirection.Input, m_params.GetParameterByName("NHS
Number").m_queryStringData))
objCommand.Parameters.Add(createParameter("@SQLRII D", SqlDbType.VarChar,
ParameterDirection.Input, m_params.GetParameterByName("SQLRI
ID").m_queryStringData))
objCommand.Parameters.Add(createParameter("@Hospit alNumber",
SqlDbType.VarChar, ParameterDirection.Input,
m_params.GetParameterByName("Hospital Number").m_queryStringData))
objCommand.Parameters.Add(createParameter("@Hospit alType",
SqlDbType.VarChar, ParameterDirection.Input,
m_params.GetParameterByName("Hospital").m_queryStr ingData))

' set our command object
objCommand.CommandText = strSQL

' execute
objReader = objCommand.ExecuteReader

' get values from reader object
'While objReader.Read()
' intCDSPatientID = objReader("CDSPatientID")
' intErrorCode = objReader("ErrorCode")
'End While
' Close reader
objReader.Close()
' try to extract parameters
intCDSPatientID = objCommand.Parameters("@CDSPatientID").Value()
intErrorCode = objCommand.Parameters("@ErrorCode").Value()
Response.Write("CDSPatientID: " & intCDSPatientID)
Response.Write("<BR>")
Response.Write("ErrorCode: " & intErrorCode)
Response.End()
Nov 18 '05 #3

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

Similar topics

2
by: Tavish Muldoon | last post by:
What a pain trying to insert data into a table from a stored proc. My webform asks for 16 pieces of data - which then gets written to the database. I found this easier than the crap below...
11
by: ColdCanuck | last post by:
Greetings! I am VERY new to DB2 but not Orable, Sybase and SQL Server. I am trying to call a stored procedure via VB 6 and ADO/OLEDB. But when I try to execute
2
by: Mike Hutton | last post by:
I have a rather odd problem. I have a SP which uses temp. tables along the way, and then returns a table of results: CREATE PROCEDURE dbo.usp_myproc( @pNameList VARCHAR(6000) ) AS
1
by: Eric Land | last post by:
Help! I'm trying to call a parameterized stored proc in ASP.NET in VB. I am creating a command object and creating a parametr list, and assigning a value from a session variable (this is working)...
6
by: David Lozzi | last post by:
Here is the proc: CREATE PROCEDURE . @CID as int, @Netname as nvarchar(25), @Return as int OUTPUT AS IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND...
4
by: Andrew Baker | last post by:
I have the following code that calles a stored proc in SQLServer. When the output parameter @custref is null (System.DBNull) I cant seem to find a test for this and I get an exception. I know I...
6
by: Paul M | last post by:
Hi All, I'm currently writing a z/OS DB2 Stored Proc in C, using an example from the IBM Stored Procedure guide (SG24-7083-00). The database calls to read and update the database work...
2
by: Rob Eventine | last post by:
hi all, how does one go about changing the details in the aspnetdb dbase using the supplied stored procs? i am using vb as my language but i have no idea how to use them, how to call them or...
0
by: mirandacascade | last post by:
Questions toward the bottom of the post. Situation is this: 1) Access 97 2) SQL Server 2000 3) The Access app: a) sets up pass-thru query b) .SQL property of querydef is a string, the...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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...

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.