views:

111

answers:

1

I am having trouble calling the following stored procedure. When I call ExecuteReader, I am getting the error 'Incorrect syntax near 'GetAverages2'. I can create the stored procedure and call it from TSQL. I can't seem to get it working from ADO:

CREATE PROCEDURE GetAverages2
    @CompanySize INT,
    @Q1   FLOAT OUT,
    @Q2   FLOAT OUT
AS  
    SELECT @Q1 = 1, @Q2 = 2
GO

    string connectionString = ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand("GetAverages2", connection))
        {
            command.Parameters.Add("@CompanySize", System.Data.SqlDbType.Int).Value = int.Parse(Request.QueryString["CompanySizeId"]);
            command.Parameters.Add("@Q1", System.Data.SqlDbType.Float).Direction = System.Data.ParameterDirection.Output;
            command.Parameters.Add("@Q2", System.Data.SqlDbType.Float).Direction = System.Data.ParameterDirection.Output;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
+2  A: 

The SqlCommand.CommandType defaults to CommandType.Text, so it's trying to execute the text "GetAverages2" as a raw SQL statement. Add this line just after you create the command:

command.CommandType = CommandType.StoredProcedure;
Matt Hamilton
damn, I'm stupid
MedicineMan
I just fell into the same trap. I'm so used to using stored procedures, I can't imagine writing raw SQL. FYI, in SQL 2008 the error message returned in this scenario has changed to "Procedure or function '<proc name>' expects parameter '<param name>', which was not supplied".
Jeremy Seghi