Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Visual Basic 2003 get return value from sql server stored procedure

Posted on 2011-02-18
5
Medium Priority
?
642 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 668 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 668 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 664 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

636 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