0

I'm trying to get a value from Microsoft SQL Server stored procedure. Totally I need to return two values. As you see below one of them is accessible by return and the other is accessible by a output parameter. I run this code in WPF(C#):

    public int InsertCustomerEntranceLogWithOptions(String cardNumber,int buy, int score, out int logID)
    {
        SqlCommand command = new SqlCommand(
            @"
            --declare @id int, @logID int                    

            exec @res = Insert_CustomerEntranceLog_WithOptions 
                @cardNumber, @buy, @score, @logID

            --set @logID = @id",
            myConnection);

        command.CommandType = CommandType.Text;
        command.Parameters.Add("@cardNumber", SqlDbType.NVarChar).Value = cardNumber;
        command.Parameters.Add("@buy", SqlDbType.Int).Value = buy;
        command.Parameters.Add("@score", SqlDbType.Int).Value = score;

        command.Parameters.Add("@logID", SqlDbType.Int).Value = 0;
        command.Parameters["@logID"].Direction = ParameterDirection.Output;      

        command.Parameters.Add("@res", SqlDbType.Int).Value = 0;
        command.Parameters["@res"].Direction = ParameterDirection.Output;

        int res = (int)RunNonQuery(command);
        logID = (int)command.Parameters["logID"].Value;

        return res;
    }

and RunNonQuery Method is:

    public Object RunNonQuery(SqlCommand command, String returnVariableName = "@res")
    {
        Object result = null;
        if (openConnection())
        {
            try
            {
                command.ExecuteNonQuery();

                if (returnVariableName.Length != 0)
                {
                    result = command.Parameters[returnVariableName].Value;
                    //command.Dispose();
                }
                else
                {
                    //command.Dispose();
                    return 1;
                }
            }
            catch (TimeoutException ex)
            {
                //Log
            }
            catch (Exception ex)
            {
                Utility.LogExceptionError(ex, 1, 1, 1);
                //throw ex;
            }

            closeConnection();
        }


        return result;
    }

I'm sure that my RunNonQuery works properly. I test it a lot. But When I InsertCustomerEntranceLogWithOptions method, I Get this error:

An unhandled exception of type 'System.IndexOutOfRangeException' 
occurred in System.Data.dll

Additional information: An SqlParameter with ParameterName 'logID' 
is not contained by this SqlParameterCollection.

It seems that I do not add SqlParameter but as you see LogID is inserted. what is wrong? I also remove comments in SQL command and then run it but steel I see the error.

3
  • 1
    @ missing in parameter logID Commented Jun 25, 2014 at 12:35
  • oh my god! I go and test it. thanks a lot. Commented Jun 25, 2014 at 12:39
  • Thank you every body. All of you help me, But I have to choose one of your answers. But +1 for all of you. Commented Jun 25, 2014 at 13:26

3 Answers 3

2

Seems like you forget the @ prefix for your sql parameter :

logID = (int)command.Parameters["@logID"].Value;

Sign up to request clarification or add additional context in comments.

Comments

1

May be that your logID = (int)command.Parameters["logID"].Value; does not access the logId because it should be named @logID, as you've added it like:

command.Parameters.Add("@logID", SqlDbType.Int).Value = 0;
command.Parameters["@logID"].Direction = ParameterDirection.Output; 

and '@' must be part of the parameter name - Is it necessary to add a @ in front of an SqlParameter name?

Comments

0

@ is missing, update like the following

logID = (int)command.Parameters["@logID"].Value;

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.