MS SQL Parameterized Stored Procedure Too Many Arguments Specified?

I generally work with Oracle, so please forgive my question. I have a ASP.NET web app calling a MS SQL stored procedure. Although the sp arguments are exactly as needed in the .net class method, I get a error message complaining about too many arguments.Help!

1. I think it might be the 'Output' causing trouble.
2. Unsure if my 'using' statements might need improvement

[sp]
ALTER PROCEDURE [dbo].[sp_Inventory_TEST]
(			
	@Quantity		int, 
	@LocationCity	nvarchar(150), 
	@LocationState	nvarchar(150), 
	@StockNumber	nvarchar(100), 	
	@Id				int OUTPUT
)
AS
BEGIN		
	....	
END

Open in new window


[asp class]
List<string> spMessageList = new List<string>(); 

using (SqlConnection cn = new SqlConnection(server))
{
    cn.Open();

    string sql = "sp_Inventory_TEST";
    SqlCommand command = new SqlCommand(sql, cn);
    command.CommandType = CommandType.StoredProcedure;

    foreach (DataRow row in dt.Rows)
    {
       if (row != null)
       {                       
           int quantity = 1; //NOT NULL
           string locationCity = row["City"].ToString();
           string locationState = row["State"].ToString();
           string stockNumber = row["Unit Number"].ToString();                      
                     
     command.Parameters.Add("@Quantity", SqlDbType.Int).Value = quantity;
     command.Parameters.Add("@LocationCity", SqlDbType.NVarChar).Value = locationCity;
     command.Parameters.Add("@LocationState", SqlDbType.NVarChar).Value = locationState;
     command.Parameters.Add("@StockNumber", SqlDbType.NVarChar).Value = stockNumber;
     command.Parameters.Add("@Id", SqlDbType.Int).Direction = ParameterDirection.Output;

     SqlDataReader dr = command.ExecuteReader();

     spMessageList.Add(command.Parameters["@Id"].Value.ToString());

     dr.Close();
    }
 }                
}

Open in new window

WorknHardrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QuinnDexCommented:
try it like that

ALTER PROCEDURE [dbo].[sp_Inventory_TEST]
(			
	@Quantity		int, 
	@LocationCity	nvarchar(150), 
	@LocationState	nvarchar(150), 
	@StockNumber	nvarchar(100), 	
	@Id				int 
)
AS
BEGIN		
	....	
END
          

Open in new window

0
WorknHardrAuthor Commented:
I need the Output. Now I get this message:

"System.Data.SqlClient.SqlException: The formal parameter "@Id" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output."
0
WorknHardrAuthor Commented:
I changed the SqlParameter Id below and got the using (SqlCommand command..) in the correct place and ditched the SQLDataReader (copy-n-paste error)

[Working Code]
ALTER PROCEDURE [dbo].[sp_Inventory_TEST]
(			
	@Quantity		int, 
	@LocationCity	nvarchar(150), 
	@LocationState	nvarchar(150), 
	@StockNumber	nvarchar(100), 	
	@Id				int OUTPUT
)
AS
BEGIN		
	INSERT...

        SET @Id = Scope_Identity()*/	
END

using (SqlConnection cn = new SqlConnection(server))
{
    cn.Open();

    foreach (DataRow row in dt.Rows)
    {
       if (row != null)
       {                       
           int quantity = 1; //NOT NULL
           string locationCity = row["City"].ToString();
           string locationState = row["State"].ToString();
           string stockNumber = row["Unit Number"].ToString();     

      using (SqlCommand command = new SqlCommand("sp_Inventory_TEST", cn))
      {
        command.CommandType = CommandType.StoredProcedure;                 
                     
        command.Parameters.Add("@Quantity", SqlDbType.Int).Value = quantity;
        command.Parameters.Add("@LocationCity", SqlDbType.NVarChar).Value = locationCity;
        command.Parameters.Add("@LocationState", SqlDbType.NVarChar).Value = locationState;
        command.Parameters.Add("@StockNumber", SqlDbType.NVarChar).Value = stockNumber;

        SqlParameter Id = new SqlParameter("@Id", 0);
        Id.Direction = ParameterDirection.Output;
        command.Parameters.Add(Id);

        command.ExecuteNonQuery();
    }
 }                
}
                                  

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Anthony PerkinsCommented:
It is supposed to be ParameterDirection.Output, but the following line is not correct:
spMessageList.Add(command.Parameters["@Id"].Value.ToString());

Why are you adding this parameter again?  All you have too do is retrieve it.

Also, you may have trouble reading the output value until you close the result set.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WorknHardrAuthor Commented:
Yeah, I got the "too many arguments' error satisfied in my last 'Working Code' post. No more errors!

Now, I'm trying to get the 'Status' and 'VinOut' into two Lists, but they are Empty.

List<string> vinInserts = new List<string>();
List<string> vinUpdates = new List<string>();   


using (SqlCommand command = new SqlCommand("sp_Inventory_TEST", cn))
      {
        command.CommandType = CommandType.StoredProcedure;                 
                     
        command.Parameters.Add("@Quantity", SqlDbType.Int).Value = quantity;
        command.Parameters.Add("@LocationCity", SqlDbType.NVarChar).Value = locationCity;
        command.Parameters.Add("@LocationState", SqlDbType.NVarChar).Value = locationState;
        command.Parameters.Add("@StockNumber", SqlDbType.NVarChar).Value = stockNumber;

        SqlParameter status = new SqlParameter("@Status", SqlDbType.NVarChar, 10);
        status.Direction = ParameterDirection.Output;
        command.Parameters.Add(status);

        SqlParameter vinOut = new SqlParameter("@VinOut",SqlDbType.NVarChar, 30);
        vinOut.Direction = ParameterDirection.Output;
        command.Parameters.Add(vinOut);

        command.ExecuteNonQuery();

        string stat = command.Parameters["@Status"].Value.ToString();
        string _vin = command.Parameters["@VinOut"].Value.ToString();

           if (stat == "INSERT")
                  vinInserts.Add(_vin); //EMPTY

           if (stat == "UPDATE")
                  vinUpdates.Add(_vin);  //EMPTY
    }

Open in new window

0
Anthony PerkinsCommented:
Please post your sp_Inventory_TEST Stored Procedure as it stands now with the additional parameters.
0
WorknHardrAuthor Commented:
I'm not using the sp methodology anymore and closing this question, thank you for helping...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.