Solved

Visual Basic 2003 get return value from sql server stored procedure

Posted on 2011-02-18
5
636 Views
Last Modified: 2012-05-11
I am trying to call a stored procedure from some Visual Basic 2003 code and I cant figure out what I am doing incorrectly in the VB Code. The stored procedure that I am calling is returning a value. I want to evaluate what the stored procedure has returned.  

*************************Here is my Visual Basic 2003 code*************

Private Function bolEligibility() As Boolean
        Dim strSql1 As New StringBuilder
        Dim strMessage as string
        strSql1.Append("exec uspCHK_Eligibility  '" & ApplNo & "'")
        Dim cnnConnection1 As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        Dim objCommand1 As SqlCommand = New SqlCommand(strSql1.ToString(), cnnConnection1)
        Dim objDataAdapter1 As SqlDataAdapter = New SqlDataAdapter
        Dim objDataSet1 As DataSet = New DataSet
        objCommand1.CommandType = CommandType.Text
        objDataAdapter1.SelectCommand = objCommand1
        objDataAdapter1.Fill(objDataSet1)        
        If objDataSet1.Tables(0).Rows(0)("App_No").ToString() <> "-1" Then
            Return True
        Else
             strMessage = "not eligible."
            Return False
        End If
    End Function

*****Here is how the SQL SERVER stored procedure looks**********

USE [TestDB]
GO
/****** Object:  StoredProcedure [dbo].[uspCHK_Eligibility]    Script
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[uspCHK_Eligibility]
(
      @ApplNo numeric(18,0)
)
AS
BEGIN
      SET NOCOUNT ON;
      IF EXISTS (SELECT * FROM tblLoan_Appl (NOLOCK) WHERE Appl_No = @ApplNo AND Status = 'N')
            Return @ApplNo
      ELSE
            Return -1
END



0
Comment
Question by:michiganblkman
5 Comments
 
LVL 7

Accepted Solution

by:
mkobrin earned 167 total points
ID: 34924881
change this
BEGIN
      SET NOCOUNT ON;
      IF EXISTS (SELECT * FROM tblLoan_Appl (NOLOCK) WHERE Appl_No = @ApplNo AND Status = 'N')
            Return @ApplNo as App_No
      ELSE
            Return -1 as App_No
END
 your app is looking for a name App_No and @ApplNo will not return a name
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 167 total points
ID: 34924933
Another thing:

objCommand1.CommandType = CommandType.Text
 
must be

objCommand1.CommandType = CommandType.StoredProcedure
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 166 total points
ID: 34924942
stored procedures can only return integers via the return code method....

you are trying to return a numeric...


you appear to be trying to access the value as it if had been returned as a result set...

in which case

IF EXISTS (SELECT * FROM tblLoan_Appl (NOLOCK) WHERE Appl_No = @ApplNo AND Status = 'N')
            Return @ApplNo
      ELSE
            Return -1


should be written as

    Select case when not exists ((SELECT * FROM tblLoan_Appl (NOLOCK) WHERE Appl_No = @ApplNo AND Status = 'N'))
                      then -1
                     else @applid end as applid
0
 

Author Comment

by:michiganblkman
ID: 34925146
Thanks for the rapid and insightful assistance so far!

How can I change my code to access the return value without makeing changes to the stored procedure?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34925174
Another remark: when you just return one value, you should use executeScalar
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question