Search code examples
c#.netodbcsystem.data

OdbcDataReader not able to see parameters when executed


When trying to execute a stored procedure using System.Data.Odbc; my application crashes because it is unable to find the parameter "@report".

This is my code:

DataTable information = new DataTable();
OdbcCommand cmd = new OdbcCommand(@"usp_BS_REPORT_LINE_LIST", _conn);

cmd.CommandType = CommandType.StoredProcedure;

OdbcParameter parameter = new OdbcParameter("@report",OdbcType.VarChar);
parameter.Value = "rep1";
cmd.Parameters.Add(parameter);

_conn.Open();
OdbcDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
    information.Load(dataReader);
}

_conn.Close();

As you can see I have declared the parameter, how do I get around this?

Exact error message:

ERROR [42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Procedure or function 'usp_BS_REPORT_LINE_LIST' expects parameter '@report', which was not supplied.

Edit: As requested by Steve, the procedure

CREATE PROC dbo.usp_BS_REPORT_LINE_LIST 
    @report varchar(128)    -- report to display
AS 

SET NOCOUNT ON

SELECT 
    s.REPORT_SECTION_NAME, l.REPORT_LINE_NAME, l.REPORT_LINE_USER_NAME
FROM
    BS_REPORT r
    INNER JOIN BS_REPORT_SECTION s ON s.BS_REPORT_ID=r.BS_REPORT_ID
    INNER JOIN BS_REPORT_LINE l ON l.BS_REPORT_SECTION_ID=s.BS_REPORT_SECTION_ID
WHERE 
    r.REPORT_NAME=@report
ORDER BY 
    s.BS_REPORT_SECTION_ID, l.REPORT_LINE_NAME

RETURN 0

GO`

Solution

  • Have you tried replacing

    OdbcCommand cmd = new OdbcCommand(@"usp_BS_REPORT_LINE_LIST", _conn);
    

    With

    OdbcCommand cmd = new OdbcCommand("{call usp_BS_REPORT_LINE_LIST(?)}", _conn);