MedicineMan
MedicineMan

Reputation: 15314

Syntax error when calling a stored procedure. I can't see what the problem is

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();

Upvotes: 3

Views: 542

Answers (1)

Matt Hamilton
Matt Hamilton

Reputation: 204129

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;

Upvotes: 2

Related Questions