2

I'm trying to execute a stored procedure (against SQL Server 2005 through the ODBC driver) and I recieve the following error:

Procedure or Function 'GetNodeID' expects parameter '@ID', which was not supplied.

@ID is the OUTPUT parameter for my procedure, there is an input @machine which is specified and is set to null in the stored procedure:

ALTER PROCEDURE [dbo].[GetNodeID] 
@machine nvarchar(32) = null,
@ID int OUTPUT
AS
BEGIN
SET NOCOUNT ON;

IF EXISTS(SELECT * FROM Nodes WHERE NodeName=@machine)
BEGIN
    SELECT @ID = (SELECT NodeID FROM Nodes WHERE NodeName=@machine)
END
ELSE
BEGIN
    INSERT INTO Nodes (NodeName) VALUES (@machine)
    SELECT @ID = (SELECT NodeID FROM Nodes WHERE NodeName=@machine)
END
END

The following is the code I'm using to set the parameters and call the procedure:

        OdbcCommand Cmd = new OdbcCommand("GetNodeID", _Connection);
        Cmd.CommandType = CommandType.StoredProcedure;

        Cmd.Parameters.Add("@machine", OdbcType.NVarChar);
        Cmd.Parameters["@machine"].Value = Environment.MachineName.ToLower();

        Cmd.Parameters.Add("@ID", OdbcType.Int);
        Cmd.Parameters["@ID"].Direction = ParameterDirection.Output;

        Cmd.ExecuteNonQuery();
        _NodeID = (int)Cmd.Parameters["@Count"].Value;

I've also tried using Cmd.ExecuteScalar with no success. If I break before I execute the command, I can see that @machine has a value.

If I execute the procedure directly from Management Studio, it works correctly.

Any thoughts? Thanks

APC
  • 144,005
  • 19
  • 170
  • 281
Aaron
  • 367
  • 1
  • 7
  • 16

3 Answers3

7

Try replacing :

OdbcCommand Cmd = new OdbcCommand("GetNodeID", _Connection);
Cmd.CommandType = CommandType.StoredProcedure;

With :

OdbcCommand Cmd = new OdbcCommand("{call GetNodeID(?,?)}", _Connection);

More info :

http://support.microsoft.com/kb/310130

Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
0

Stored procedure with input parameters and ODBC Connection:

create a stored procedure:

create procedure proc_name @parm1 varchar(20), @parm2 varchar(10) as begin insert into table_name values(@parm1,@parm2);end


This code works in SQL Server.

    private void button1_Click(object sender, EventArgs e)
    {
        string name = txtname.Text;
        string num = txtnum.Text;
        OdbcConnection con = new OdbcConnection("dsn=naveenk_m5");
        OdbcCommand cmd = new OdbcCommand("{call proc1(?,?)}",con);
        cmd.Parameters.Add("@parm1", OdbcType.VarChar).Value=name;
        cmd.Parameters.Add("@parm2", OdbcType.VarChar).Value = num;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("inserted a row");

    }
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Naveen
  • 1
  • 1
0

I'm not exactly sure what you mean by

there is an input @machine which is specified and is set to null in the stored procedure

In your proc's signature, this line:

@machine nvarchar(32) = null

doesn't mean that you're setting @machine to null inside the proc - it means you're assigning a default value to be used in case the parameter is missing (in this case, null is the value to be used for a missing param).

Getting the error about @ID being missing would happen if you were calling this stored procedure without passing any parameters at all (@machine would not be flagged as a problem since it has a default value defined). Your code example looks fine to me - are you sure the stored proc isn't being called from somewhere else in your program (somewhere where no parameters are being added)?

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334