0

I'm pulling my hair out!!

I wrote a simple scalar function and now I'm tring to execute it from vb.net and get the result back but it always shows up as zero, even though when I call it in SSMS it returns the value of 1, which is what I expect.

The function looks like this:

Alter FUNCTION [dbo].[CheckParity]
(@test char(1)  )
RETURNS int
AS
BEGIN
declare @result int
IF (SELECT SUM(REVENUEAMOUNT) FROM CommissionDetail WHERE  RevenueType IN('P','S')) =
   (SELECT SUM(SERVICEREVENUE + PRODUCTREVENUE) FROM SalesmanAttainment)
    set @result = 1
else  
    set @result = 0
return @result
END

I grabbed the vb code from the web:

                sql = "dbo.CheckSalesAttainmentParity"
                Dim count As Integer

                cnn = New OleDbConnection(conn.ConnectionString)
                Try
                    cnn.Open()
                    cmd = New OleDbCommand(sql, cnn)
                    cmd.Parameters.Add("test", OleDbType.Integer)
                    cmd.Parameters("test").Direction = ParameterDirection.ReturnValue
                    cmd.ExecuteScalar()
                    count = cmd.Parameters("test").Value
                    cmd.Dispose()
                    cnn.Close()                   
                sql = "dbo.CheckSalesAttainmentParity"
                Dim count As Integer

                cnn = New OleDbConnection(conn.ConnectionString)
                Try
                    cnn.Open()
                    cmd = New OleDbCommand(sql, cnn)
                    cmd.Parameters.Add("test", OleDbType.Integer)
                    cmd.Parameters("test").Direction = ParameterDirection.ReturnValue
                    cmd.ExecuteScalar()
                    count = cmd.Parameters("test").Value
                    cmd.Dispose()
                    cnn.Close()

I'm totally stumped. I'd appreciate all ideas

2 Answers 2

1

Duh!

I left out this line of code:

cmd.CommandType = CommandType.StoredProcedure
Sign up to request clarification or add additional context in comments.

3 Comments

Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question.
If this is the answer to the question, please mark it as accepted so that people will know that it is the answer (click the checkmark on the sidebar to the left). It looks very like an update to the question as Mike said above.
@Mike: It answers the question.
0

You're using a SQL Server database so I'm not sure if there was a particular reason you used OleDbConnection instead of SqlConnection, but try this.

    Dim Sql As String = "select Sample.dbo.CheckParity('a')"
    Dim count As Integer
    Dim cnn As SqlConnection
    Dim cmd As SqlCommand

    Try
        cnn = New SqlConnection(conn.ConnectionString)
        cnn.Open()

        cmd = New SqlCommand(Sql, cnn)

        count = CInt(cmd.ExecuteScalar())

        Console.WriteLine(count)
        Console.ReadKey()
    Finally
        cmd.Dispose()
        cnn.Close()
    End Try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.