title: SqlCommand Parameterized SQL error: Must declare the scalar variable "@ObjectType"
body: <p>I'm getting this error:</p>
  
  <blockquote>
    <p><em>Must declare the scalar variable "@ObjectType".</em></p>
  </blockquote>
  
  <p>Any idea why? The query works if I hard code values.</p>
  
  <pre><code>public static DataSet GetDatabaseObjectHistory(string objectType, 
                                                 string schemaName, 
                                                 string objectName, 
                                                 string msConnectionString)
  {
     using (SqlConnection sqlConnection = new SqlConnection()
                          {
                             ConnectionString = msConnectionString
                          })
     {
        sqlConnection.Open();
  
        using (DataSet ds = new DataSet())
        {
            #region sqlGetSchemaObjects
            const string sql = @"SELECT
                                             RowId
                                            ,EventTime
                                            ,LoginName
                                            ,UserName
                                            ,DatabaseName
                                            ,SchemaName
                                            ,ObjectName
                                            ,ObjectType
                                            ,DDLCommand
                                          FROM
                                             Audit_Log
                                          WHERE
                                              Audit_Log.ObjectType = @ObjectType AND
                                              Audit_Log.SchemaName = @SchemaName AND
                                              Audit_Log.ObjectName = @ObjectName";
            #endregion
  
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            sqlCommand.CommandType = CommandType.Text;
  
            sqlCommand.Parameters.AddWithValue("@ObjectType", objectType);
            sqlCommand.Parameters.AddWithValue("@SchemaName", schemaName);
            sqlCommand.Parameters.AddWithValue("@ObjectName", objectName);
  
            using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection))
            {
                sqlDataAdapter.Fill(ds, sql);
            }
  
            ds.Tables[0].TableName = "Object History";
            return ds;
        }
     }
     return null;
  }
  </code></pre>