Parameter sent to SQL Server stored procedure not recognized

1

I am in Visual Studio 2010 using C# and connection to a SQL Server 2008 database. I am calling a stored procedure and passing it a parameter. I am getting the error message below.

I have checked the parameter being passed and the name and value are correct and the connection is open, the stored procedure name is also correct. If I set a default value for the parameter it does return the corresponding rows. Below the error message I have put the code that is being executed. The values being passed to Parameters.AddWithValue are ParmName = @DetailCategoryID, ParmValue = 2. When I execute the exact same code using Visual Web Developer 2008 Express Edition it works perfectly.

System.Data.Odbc.OdbcException (0x80131937): ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Procedure or function 'spGET_Detail' expects parameter '@DetailCategoryID', which was not supplied.\r\n at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)\r\n at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)\r\n at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)\r\n at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)\r\n at System.Data.Odbc.OdbcCommand.ExecuteReader()\r\n
at DataAccess.OpenReader() in C:\Development\DetailRetrieval\DetailRetrieval\Classes\DataAccess.cs:line 1252

The code

this.DB_Comm.Parameters.AddWithValue(ParmName, ParmValue);

Stored procedure:

ALTER PROCEDURE spGET_Detail
    @DetailCategoryID smallint
AS
BEGIN
    SET NOCOUNT ON;

    SELECT F.* FROM dbo.tbDetail_MSTR AS F
    WHERE (F.DetailCategoryID = @DetailCategoryID)
    ORDER BY [F].[Description]
END
GO

I would greatly appreciate any help anybody can offer.

Thank You

c#
.net
sql-server
asked on Stack Overflow Feb 3, 2012 by Marty Goetz • edited Feb 3, 2012 by marc_s

3 Answers

3

The error gives you all the information you need:

Procedure or function 'spGET_Detail' expects parameter '@DetailCategoryID',

But you said

ParmName = @DetailID

Edit: I just realized that you are using the ODBC driver to connect to SQL server. The ODBC driver uses question marks as parameter place holders, not named parameters. So your query should not have @anything. The parameters are replaced based on order, not name.

From: http://msdn.microsoft.com/en-us/library/bbw6zyha.aspx

The syntax for parameter placeholders depends on the data source. This example shows placeholders for a SQL Server data source. Use question mark (?) placeholders for System.Data.OleDb and System.Data.Odbc parameters.

That said, unless you are going to be connecting to a lot of other database types, you probably should use SqlClient, not ODBC.

answered on Stack Overflow Feb 3, 2012 by Chris Shain • edited Feb 4, 2012 by Chris Shain
2

Use this:

his.DB_Comm.Parameters.AddWithValue("DetailCategoryID", 2);
answered on Stack Overflow Feb 3, 2012 by Mithrandir
0

I was having this same issue - Exception said the parameter wasn't there when it was, regardless of having the '@' or not in the parameter name.

I resolved it by setting the command type of the SqlCommand object to StoredProcedure:

DB_Comm.CommandType = CommandType.StoredProcedure;

PS - It doesn't matter if you have the @ in the parameter name with this method. I tested with some @parameters and others without in the same sproc call and it worked.

answered on Stack Overflow Jan 29, 2015 by Kevin Pope

User contributions licensed under CC BY-SA 3.0