473,386 Members | 1,798 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.

Unable to get a Return value after executing a stored procedure

1
Can someone help me, I'm going round in circles with this and my head is cabbaged !

I am using visual studio 2005 & VB & MS SQL 2005

I am trying to execute a stored procedure from within a program. I want to return values. I can get a 0 or 1 returned. I don't seem to be able to get any other value returned.

below is my VB code and the stored procedure. I would really appreciate if someone would have a look that knows how to do this. Thanks if you help.

Public Function ExSPretInt(ByVal procName As String, ByVal userno As String, ByRef dbCon As SqlClient.SqlConnection) As String
Dim status As Integer = 999
Dim command As SqlCommand = New SqlCommand

dbcon.Open()
With command
.Connection = dbCon
.CommandText = procName
.CommandType = CommandType.StoredProcedure

End With

Dim p1 As SqlParameter
p1 = command.Parameters.Add(New SqlClient.SqlParameter("@status", SqlDbType.NVarChar, 10, ParameterDirection.Output))
p1.Value = status.ToString

Dim p2 As SqlParameter
p2 = command.Parameters.Add("@user", SqlDbType.Int, 6)
p2.Value = CInt(userno)

Dim txt As String = "hello"
Dim p3 As SqlParameter
p3 = command.Parameters.Add("@txt", SqlDbType.NVarChar, 8, ParameterDirection.Output)
p3.Value = txt.ToString


Dim x As String = Nothing
Try

command.ExecuteNonQuery()
If command.Parameters("@txt").Value = "Error" Then
x = "Error"
Else
x = (command.Parameters("@txt").Value).ToString
End If

Catch ex As SqlException
Dim exerror As String = "exception error"
End Try

Return x

End Function

************************************************** ****************************************

STORED PROCEDURE



set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[deleteUser] ( @status int output, @user int, @txt nvarchar(8) )
AS

BEGIN TRAN

delete games where gamedesc = 'test'

IF @@ERROR = 0
begin
commit tran
set @status = 6
set @user = 12
set @txt = 'Record committed'
return
end
else
begin
ROLLBACK TRAN
set @status = 13
set @user = 15
Set @txt = 'Error'
return
end
Nov 7 '08 #1
4 2954
Plater
7,872 Expert 4TB
Hmm well you do some curious behavior here.
According to your stored procedure, the only value that should "return" anything is the @status value.
For some reason you apply an initial value to that parameter, not sure why.

Then after you execute your stored procedure, you don't even look at your only output parameter ( "@status" ).

I think once you sort yourself out and get correct program flow happening, you will get your values back
Nov 7 '08 #2
hi all,
i'm using vb.net 2003 with sql server 2000 , i write one stored procedure with one input parameter and one output parameter when i try to execute in Query analyser it's working fine, where as thru VB.net code i got wrong results, my code is below.

dim intItemcode as integer = 308

cmd = New SqlCommand("AvailItemQty", cn)

With sqlparam
.ParameterName = "@sntItemCode"
.SqlDbType = SqlDbType.SmallInt
.Value = intItemCode

.ParameterName = "@dblQty"
.SqlDbType = SqlDbType.Float
.Direction = ParameterDirection.Output

End With

With cmd
.Parameters.Add(sqlparam)
.CommandType = CommandType.StoredProcedure
StockQty = CType(cmd.Parameters("@dblQty").Value, Double)
End With
i got result 308 instead of 1200. i don't know where is the problem?

Thanks in Adv.
Ganesh
Mar 11 '09 #3
I think this should work

With sqlparam1
.ParameterName = "@sntItemCode"
.SqlDbType = SqlDbType.SmallInt
.Value = intItemCode
End with

With sqlParam2
.ParameterName = "@dblQty"
.SqlDbType = SqlDbType.Float
.Direction = ParameterDirection.Output
End With

With cmd
.Parameters.Add(sqlparam1)
.Parameters.Add(sqlparam2)

.CommandType = CommandType.StoredProcedure
StockQty = CType(cmd.Parameters("@dblQty").Value, Double)
Mar 11 '09 #4
hi ,
Thanx for u'r reply.
It's not working.. i got result as 0 instead of 1200.

Stored procedure code is below

create proc AvailItemQty @sntItemCode smallint , @dblQty float output AS

select @dblQty = sum(Res.openTotQty+Res.RecTotQty - Res.IssTotQty)
from
( select 0 as OpenTotQty, RecDet.dblTotBaseQty as RecTotQty,
0 AS IssTotQty FROM tblReceiptMaster RecMas, tblReceiptDetails RecDet WHERE RecMas.intRecNo = RecDet.intRecNo and RecMas.blnDeleted = 0 and RecDet.sntItemCode = @sntItemCode

union
select 0 as OpenTotQty, InRetDet.dblTotBaseQty as RecTotQty, 0 AS IssTotQty FROM tblInhouseReturnMaster InRetMas, tblInhouseReturnDetails InRetDet WHERE InRetMas.intIRNo = InRetDet.intIRNo and InRetMas.blnDeleted = 0 AND InRetDet.sntItemCode = @sntItemCode

union

select 0 as OpenTotQty,0 as RecTotQty,RetDet.dblTotBaseQty as IssTotQty FROM tblReturnMaster RetMas, tblReturnDetails RetDet
WHERE RetMas.intRetNo = RetDet.intRetNo and RetMas.blnDeleted = 0 AND RetDet.sntItemCode = @sntItemCode

union

select 0 as OpenTotQty,0 AS RecTotQty,InIssDet.dblTotBaseQty as IssTotQty FROM tblInhouseIssueMaster InIssMas, blInhouseIssueDetails InIssDet WHERE InIssMas.intIINo = InIssDet.intIINo and InIssMas.blnDeleted = 0 AND InIssDet.sntItemCode = @sntItemCode
union

select 0 as OpenTotWt, 0 as RecTotWt, WastDet.dblTotBaseQty as IssTotWt FROM tblWastageMaster WastMas,tblWastageDetails WastDet
WHERE WastMas.intWNo = WastDet.intWNo and WastMas.blnDeleted = 0 AND WastDet.sntItemCode = @sntItemCode

union

select OpDet.dblTotBaseQty as OpenTotWt, 0 as RecTotWt,
0 as IssTotWt FROM tblOpeningMaster OpMas,tblOpeningDetails OpDet WHERE OpMas.intOpNo = OpDet.intOpNo and OpMas.blnDeleted = 0 and OpDet.sntItemCode = @sntItemCode

)Res


ganesh
Mar 12 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: JT | last post by:
how can i see a stored procedures return value in ASP??
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
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...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
5
by: Fir5tSight | last post by:
Hi All, I have a C#.NET code as follows: private void ScanInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { try { Row lRow = this.Connection.InsertScannedFile(ID);
0
by: DR | last post by:
Unable to start TSQL Debugging. Could not attach to SQL Server Process on 'srvname'. The RPC server is unavailable. I get this error when I try to run a SQL Server Project with a CLR stored...
2
by: gopi2ks | last post by:
I have one stored procedure like sp_insertEmployee Employee Table Fileds Eno int pk, ename varchar(100), designation varchar In stored Procedure After inserting the ename and...
3
by: gopi2ks | last post by:
Dear All, I have one stored procedure like sp_insertEmployee Employee Table Fileds Eno int pk, ename varchar(100), designation varchar
8
by: mcfly1204 | last post by:
I am executing a stored procedure that will always return a value, specifically an integer. The error handling within the stored procedure will return 0 if succesful, 1 on an error, and 2 for a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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
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...

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.