Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 11th, 2006, 06:51 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Procedure or function has too many arguments

Hi everybody

I am a beginner and facing the problem in calling a Stored Procedure from within my VB.NET 2005, as below:

[u]MY Stored Procedure goes like</u>;
ALTER PROCEDURE ByRoyalty @percentage int, @avgprice float Output
    AS
    SELECT @avgprice= AVG(price) FROM titles
    SELECT au_id FROM titleauthor
        WHERE titleauthor.royaltyper = @percentage
        DECLARE @numtitles Int
    SELECT @numtitles=COUNT(*) FROM titles
RETURN @numtitles

[u]My VB code is as below</u>:
Dim cmd As New SqlCommand("ByRoyalty", Conxn)
        With cmd
            .CommandType = CommandType.StoredProcedure

            .Parameters.Add("@numtitles", SqlDbType.Int)
            .Parameters(0).Direction = ParameterDirection.ReturnValue

            .Parameters.AddWithValue("@percentage", 100)
            .Parameters(1).Direction = ParameterDirection.Input

            .Parameters.Add("@avgprice", SqlDbType.Float)
            .Parameters(2).Direction = ParameterDirection.Output
        End With
        Dim dr As SqlDataReader = cmd.ExecuteReader()

Running the above vb code reports the following error:
Procedure or function ByRoyalty has too many arguments specified.

Please help

Raby


 
Old August 11th, 2006, 09:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

try to specify exactly every parameter, name, type, len. you are not specifying all the data of the parameters...

HTH

Gonzalo
 
Old August 11th, 2006, 11:07 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried but failed. I think I am not doing it in a proper way.
Should I modify the Call to the Stored Procedure, in VB, or have to make some changes in the Stored Procedure?
Please suggest me the exact parameters sequence. Thanks in anticipation.


 
Old August 11th, 2006, 12:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

I'm not an expert.. but let's try a couple of things..

order your parameters, should be in the same order at they appear in the SP..
Code:
Dim cmd As New SqlCommand("ByRoyalty", Conxn)
        With cmd
            .CommandType = CommandType.StoredProcedure
            'first parameter, include the type and len that are missing...
            .Parameters.AddWithValue("@percentage", 100)
            .Parameters(0).Direction = ParameterDirection.Input
            'second parameter, still mising the len 
           .Parameters.Add("@avgprice", SqlDbType.Float)
            .Parameters(1).Direction = ParameterDirection.Output
            'third parameter, is this a parameter or the result of the function?? I don't know about this...
            .Parameters.Add("@numtitles", SqlDbType.Int)
            .Parameters(2).Direction = ParameterDirection.ReturnValue

 
        End With
       Dim dr As SqlDataReader = cmd.ExecuteReader()
a couple of things...
 
  • your SP does 3 things, but neither one of them are relateds
  • why execute a Select that doesn't do anything inside the SP ( SELECT au_id FROM titleauthor WHERE titleauthor.royaltyper = @percentage)
  • to try it better can you split it to try every part by itself???


HTH

Gonzalo
 
Old August 11th, 2006, 02:33 PM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Unfortunately, the re-ordering did not work.
Also;
the AddWithValue does not allow the data type.
The other two parameters are Int and Float, which do not require the len arguments.

Any other idea, please!

 
Old August 12th, 2006, 05:01 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

THOUSANDS OF PARDONS to all the friends who tried to help.
The problem was somewhere else.

As a matter of fact, all the changes I had been making were commencing in the LOCAL COPY of my database. For this reason, my changes did not reflect in the result set.

I again aplogize everybody.

With gread regards.


 
Old November 16th, 2007, 11:38 AM
cho cho is offline
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

look at the following code. Make sure to set the sqlcommand to connection.CreateCommand()

CREATE PROCEDURE GetDocumentSummary
(
    @DocumentID int,
    @DocumentSummary nvarchar(MAX) OUTPUT
)
AS
SET NOCOUNT ON
SELECT @DocumentSummary=Convert(nvarchar(MAX), DocumentSummary)
FROM Production.Document
WHERE DocumentID=@DocumentID

VB
Private Function GetDocumentSummary( _
  ByVal documentID As Integer) As String

    ' Assumes GetConnectionString returns a valid connection string.
    Using connection As New SqlConnection(GetConnectionString())
        connection.Open()
        Dim command As SqlCommand = connection.CreateCommand()

        ' Setup the command to execute the stored procedure.
        command.CommandText = "GetDocumentSummary"
        command.CommandType = CommandType.StoredProcedure

        ' Set up the input parameter for the DocumentID.
        Dim paramID As SqlParameter = _
            New SqlParameter("@DocumentID", SqlDbType.Int)
        paramID.Value = documentID
        command.Parameters.Add(paramID)

        ' Set up the output parameter to retrieve the summary.
        Dim paramSummary As SqlParameter = _
            New SqlParameter("@DocumentSummary", _
               SqlDbType.NVarChar, -1)
        paramSummary.Direction = ParameterDirection.Output
        command.Parameters.Add(paramSummary)

        ' Execute the stored procedure.
        command.ExecuteNonQuery()
        Console.WriteLine(paramSummary.Value)
        Return paramSummary.Value.ToString
    End Using
End Function

C#
static private string GetDocumentSummary(int documentID)
{
    //Assumes GetConnectionString returns a valid connection string.
    using (SqlConnection connection =
               new SqlConnection(GetConnectionString()))
    {
        connection.Open();
        SqlCommand command = connection.CreateCommand();
        try
        {
            // Setup the command to execute the stored procedure.
            command.CommandText = "GetDocumentSummary";
            command.CommandType = CommandType.StoredProcedure;

            // Set up the input parameter for the DocumentID.
            SqlParameter paramID =
                new SqlParameter("@DocumentID", SqlDbType.Int);
            paramID.Value = documentID;
            command.Parameters.Add(paramID);

            // Set up the output parameter to retrieve the summary.
            SqlParameter paramSummary =
                new SqlParameter("@DocumentSummary",
                SqlDbType.NVarChar, -1);
            paramSummary.Direction = ParameterDirection.Output;
            command.Parameters.Add(paramSummary);

            // Execute the stored procedure.
            command.ExecuteNonQuery();
            Console.WriteLine((String)(paramSummary.Value));
            return (String)(paramSummary.Value);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Function and Procedure code_lover Oracle 2 July 24th, 2007 07:05 AM
Passing multiple arguments to function zarikiane XSLT 2 July 18th, 2006 09:35 AM
Max Arguments Sent to Stored Procedure? saturdave ASP.NET 1.0 and 1.1 Basics 3 June 15th, 2006 10:15 AM
storage PROCEDURE arguments question? AFei Classic ASP Databases 4 September 24th, 2004 04:35 AM
The isnull function requires 2 arguments. sinapra Classic ASP Databases 1 August 30th, 2004 07:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.