Results 1 to 5 of 5

Thread: (Resolved) Return Value from Stored Proc

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    (Resolved) Return Value from Stored Proc

    I am trying to get a value back from a stored procedure but I haven't been able to get the code right and the samples I have looked at have added more confusion. Here is what I have:
    vb Code:
    1. ' TestForm
    2.     Private Sub Button1_Click( _
    3.         ByVal sender As System.Object, _
    4.         ByVal e As System.EventArgs) Handles Button1.Click
    5.  
    6.         Dim myemail As String
    7.         myemail = CStr(GetEmail(1))
    8.         MessageBox.Show(myemail)
    9.  
    10.     End Sub
    11.     Private Function GetEmail(ByVal Store As Integer) As String
    12.  
    13.         Dim cn As SqlConnection = New SqlConnection
    14.         Dim cmd As SqlCommand = New SqlCommand("sproc_GetDMEmail", cn)
    15.         Dim par As SqlParameter = New SqlParameter
    16.  
    17.         cn.ConnectionString = Con
    18.         cn.Open()
    19.  
    20.         par.ParameterName = "@Store"
    21.         par.Direction = ParameterDirection.Output
    22.         par.Value = store
    23.  
    24.         cmd.CommandType = CommandType.StoredProcedure
    25.         cmd.Parameters.Add(par)
    26.         cmd.ExecuteNonQuery()
    27.  
    28.         Return cmd.Parameters.Add(par).Value.ToString
    29.  
    30.     End Function

    The error I get is The formal parameter "@Store" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.
    Last edited by FastEddie; Sep 11th, 2007 at 02:28 PM.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Return Value from Stored Proc

    What is the SP definition?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Return Value from Stored Proc

    sp definition? I am very new at this so I am not sure what you mean but I can run the stored procedure from Studio Manager with this

    EXEC sproc_GetDmEmail 1

    and get the correct result so I know it works. The entire SP looks like this:
    Code:
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE sproc_GetDMEmail 
    (@Store VARCHAR(255))
    AS
    BEGIN
    	SET NOCOUNT ON;
    Select EmailAddress FROM tblDmEmail WHERE StoreID = @Store
    END
    GO

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Return Value from Stored Proc

    Well I think that in the definition you have @Store defined as an input parameter. I also think that you want to return the result set of the query where is that set as ether a dataset or datareader in the VB code?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Return Value from Stored Proc

    Well a big DUHHHHH goes to me. Your post made me realize I just worked through this problem one project ago. Here is a sample for those of you who may find yourself with the same problem.
    vb Code:
    1. Dim strEmail As String = Nothing
    2.         Dim strSp As String = "sproc_GetDmEmail"
    3.         Dim cn As SqlConnection = New SqlConnection
    4.         Dim cmd As SqlCommand = New SqlCommand(strSp, cn)
    5.         Dim dr As SqlDataReader
    6.         cn.ConnectionString = Con
    7.         cn.Open()
    8.         cmd.CommandType = CommandType.StoredProcedure
    9.         cmd.Parameters.Add("@Store", SqlDbType.VarChar).Value = parStore
    10.         Try
    11.             dr = cmd.ExecuteReader()
    12.             While dr.Read()
    13.                 With dr
    14.                     strEmail = CStr(.Item("EmailAddress"))
    15.                 End With
    16.             End While
    17.         Catch ex As Exception
    18.             MessageBox.Show(ex.Message & vbNewLine & _
    19.             ex.Source, "Execute SP Error", MessageBoxButtons.OK)
    20.         End Try
    21.         Return strEmail

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width