Solved

Visual Basic 2003 get return value from sql server stored procedure

Posted on 2011-02-18
5
638 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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